AttributeError for unique field during ModelForm validation

98 views
Skip to first unread message

Rohit Banga

unread,
Nov 10, 2012, 8:40:54 PM11/10/12
to django...@googlegroups.com
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 = '****'

Andrejus

unread,
Nov 10, 2012, 10:12:18 PM11/10/12
to django...@googlegroups.com
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 написал:

Rohit Banga

unread,
Nov 10, 2012, 11:05:54 PM11/10/12
to django...@googlegroups.com
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. 

Andrejus

unread,
Nov 11, 2012, 2:21:17 PM11/11/12
to django...@googlegroups.com

Unfortunately I haven't got experience with abstarct models, one thing I clearly carried out form docs -  abstract model has only one clear purpose - to avoid duplicating the same fields and methods when writing code for models. Abstract model is never transformed to database table, so may not support all model's features in strightforward way. Try to make more simple design wich may seem more clear for django. You can set unique key on one or more more fields not only using unique property, but in Meta class as well (it is used on real models when you want to set unique composite key). I can advise to use multy-table model inheritance if you want to store common set of fields in a distinct database table - this way I have successfully used myself in my own project. It's simple, clear and well-working method. Django authomatcally creates all necessary pk's and fk's in the database, all models appear in admin site. And you can use ModelForms on any model you want.

воскресенье, 11 ноября 2012 г., 3:05:54 UTC+4 пользователь Rohit Banga написал:

Rohit Banga

unread,
Nov 11, 2012, 3:10:07 PM11/11/12
to django...@googlegroups.com
Thanks Andrejus. Looks like you understand my usecase. I have to use multiple tables with the same schema which is why I am using inheritance. I am using abstract base models as each model should get its own table. If I use multi-table inheritance data from all these tables ends up in one table while creating a one-to-one relationship between the two tables. I must use abstract models. But I can consider creating model forms for the child tables and then loading the model form dynamically.

I can now understand that the unique check on the abstract model would fail if the model form pointed to an abstract model. It does not know which table implementation to look at to verify if the unique constraint is satisfied.


Thanks
Rohit Banga
http://iamrohitbanga.com/


--
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/-/vH-qJUSke8gJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages