Saturday, November 10, 2012

Re: AttributeError for unique field during ModelForm validation

I want to ensure that a username is unique hence the unique constraint. Is having a ModelForm with an abstract model is supported? If not I will consider dynamically creating an instance of derived model form. But it looks a lot cleaner with the abstract class. 

On Saturday, November 10, 2012 5:12:18 PM UTC-5, Andrejus wrote:
Hi!
I would try to set unique property within "real" model, not within abstract base class. Besides django creates unique pk on each model by default, so to my mind creating additional unique field is redundant. Not quite sure, but there seems to be some restrictions on use of unique property. 

воскресенье, 11 ноября 2012 г., 0:40:54 UTC+4 пользователь Rohit Banga написал:
I noticed a strange behavior with Django and filed a bug
https://code.djangoproject.com/ticket/19271#ticket

It is suggested that I first check on the support group if the bug is valid or not. Fair Enough.

I have created a standalone project to demonstrate the problem. In order to run it you may have to create a database and configure it in settings.py though.
Code that does not work:
https://github.com/iamrohitbanga/django_ticket_19271/tree/code_not_working

Code that works:

Just creating my_id field in BaseModel class with unique=True set causes the code to fail with the stack trace below.
My usecase is a bit weird hence you would find model form inheriting from abstract models. IMHO that should not matter. The code works without unique fields but does not work with non-unique fields. I will be happy to provide more clarifications.

Below is the stacktrace for the 
"AttributeError at /create_new type object 'BaseModel' has no attribute '_default_manager' Traceback: File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "django_bug_19271/testproj/testproj/views.py" in create_new 33. if form.is_valid(): File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in is_valid 124. return self.is_bound and not bool(self.errors) File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in _get_errors 115. self.full_clean() File "/usr/local/lib/python2.7/dist-packages/django/forms/forms.py" in full_clean 272. self._post_clean() File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in _post_clean 338. self.validate_unique() File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in validate_unique 347. self.instance.validate_unique(exclude=exclude) File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in validate_unique 633. errors = self._perform_unique_checks(unique_checks) File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _perform_unique_checks 717. qs = model_class._default_manager.filter(**lookup_kwargs) Exception Type: AttributeError at /create_new Exception Value: type object 'BaseModel' has no attribute '_default_manager' Request information: GET: No GET data POST: csrfmiddlewaretoken = u'**********' my_id = u'1' name = u'Rohit' FILES: No FILES data COOKIES: csrftoken = '****'

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ZWttij8sGjQJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate