django-easymode icon indicating copy to clipboard operation
django-easymode copied to clipboard

unique_error_message has moved to model in Django 1.3

Open svisser opened this issue 13 years ago • 5 comments

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)

svisser avatar Jan 04 '12 11:01 svisser

Is that code from easymode or something else?

specialunderwear avatar Jan 04 '12 11:01 specialunderwear

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

svisser avatar Jan 04 '12 18:01 svisser

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 avatar Jan 05 '12 00:01 specialunderwear

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.

specialunderwear avatar Jan 28 '12 12:01 specialunderwear

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',),
        }),
    )

svisser avatar Jan 30 '12 09:01 svisser