specialunderwear / django-easymode

Quickly build backends for flash/flex websites with Django.
GNU General Public License v3.0
35 stars 20 forks source link

unique_error_message has moved to model in Django 1.3 #5

Open svisser opened 12 years ago

svisser commented 12 years ago

As of Django 1.3, the unique_error_message method has moved from the form to the model. For models with a unique slug field, this causes the following error upon saving:

AttributeError: 'RecipeForm' object has no attribute 'unique_error_message'

It happens here:

if qs.extra(select={'a': 1}).values('a').order_by():
    self._errors[field_name] = ErrorList([self.unique_error_message([field_name])]) <---
    bad_fields.add(field_name)
specialunderwear commented 12 years ago

Is that code from easymode or something else?

svisser commented 12 years ago

That's in easymode.i18n.admin.forms at line 126 (easymode 1.0b1).

specialunderwear commented 12 years ago

To fix this the following has to be done: create unit test that tests the error message delivered when violating the unique constraint. (This will fail because the error will occur)

Try to remove the code entirely and see if the test passes. If so, you fixed it. If not, the mechanism of where the errors come from must be understood and properly hooked for the translated fields.

I will fix it when I can find time, but maybe you can create the unit test and try the simple remove option. The test should go in easymode.tests.testcases.testforms. The test in there can serve as an example of your test.

specialunderwear commented 12 years ago

Hi I'm trying to fix this but I get 0 errors. Did you override the model form on the model or not? Please add minimal example of model, admin class and forms (if you did that) which show the error.

svisser commented 12 years ago

I've created a subclass of the SearchForm of Haystack (django-haystack==1.2.6), which is a subclass of Django's forms.Form (source: https://github.com/toastdriven/django-haystack/blob/v1.2.6/haystack/forms.py )

My custom search form looks like:

from django.db import models
from haystack.forms import SearchForm

class SingleModelSearchForm(SearchForm):
    """Search form to only search for a particular type of model."""
    model = None

    def search(self):
        sqs = super(SingleModelSearchForm, self).search()
        return sqs.models(getattr(self, 'model'))

And the actual forms:

class ProductSearchForm(SingleModelSearchForm):
    model = Product

class RecipeSearchForm(SingleModelSearchForm):
    model = Recipe

The models (Product and Recipe) are ordinary models with various translatable fields, including a translatable models.SlugField.

The admin classes of Recipe and Product have nothing special, e.g.:

@L10n(Recipe)
class RecipeAdmin(admin.ModelAdmin):
    list_display = ('title',)

    fieldsets = (
        (None, {
            'classes': ['wide'],
            'fields': ('title', 'slug', 'tags',),
        }),
        (_('Images'), {
            'classes': ['wide'],
            'fields': ('image', 'homepage_image',),
        }),
        (_('Properties'), {
            'classes': ['wide'],
            'fields': ('servings', 'preparation_time', 'product',),
        }),
        (_('Homepage'), {
            'classes': ['wide'],
            'fields': ('person', 'person_text',),
        }),
        (_('Preparation'), {
            'classes': ['wide'],
            'fields': ('ingredients', 'preparation', 'tip',),
        }),
    )