Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unique_error_message has moved to model in Django 1.3 #5

Open
svisser opened this issue Jan 4, 2012 · 5 comments
Open

unique_error_message has moved to model in Django 1.3 #5

svisser opened this issue Jan 4, 2012 · 5 comments

Comments

@svisser
Copy link

svisser commented Jan 4, 2012

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
Copy link
Owner

Is that code from easymode or something else?

@svisser
Copy link
Author

svisser commented Jan 4, 2012

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

@specialunderwear
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

svisser commented Jan 30, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants