Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19271 closed Bug (invalid)

Form validation throws AttributeError has "Base Model has no attribute _default_manager"

Reported by: iamrohitbanga@… Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: forms, models, unique constrait
Cc: iamrohitbanga@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Claude Paroz)

I have an abstract model BaseModel having a field with unique=True. BaseModel obviously has abstract=True.
Now I create a MyModel which inherits from BaseModel.

Now I have a view that tries to create a new entry in MyModel with data in post request.

    def add_new(request, id) {
       myModel = MyModel()          # I am actually creating MyModel dynamically but that should not matter
       if request.method == 'POST':
          form = MyModelForm(request.POST)
          if form.is_valid():
              mymodel.field1 = form.cleaned_data['field1']
              mymodel.field2 = form.cleaned_data['field3']
              mymodel.save()
              return render_to_response("mytemplate.html", {'form' : form},
                                        RequestContext(request))
          else:
              pass   # return some error here
    }

Now this code works fine when field1 does not have unique=True set but not when field1 has unique=True property set.
When the unique property is set then the form.is_valid() fails. It gives an AttributeError. 'BaseModel' object has no attribute '_default_manager'.

The moment I remove unique=True from my field it works perfectly fine.
Just a caveat here. I am creating MyModel object dynamically creating the object by loading the class dynamically using the approach described here.
http://stackoverflow.com/a/547867/161628

Change History (6)

comment:1 by Claude Paroz, 12 years ago

Description: modified (diff)

Reformatted, please use preview before posting.

comment:2 by Claude Paroz, 12 years ago

Resolution: invalid
Status: newclosed

Basically, your ModelForm usage seems strange. You are manually creating a model instance, then you don't use it and MyModelForm will probably create a new one. I'd suggest your first use support channels to see if your code is right, then reopen the bug with more complete code if you still think there is a bug in Django.

comment:3 by iamrohitbanga@…, 12 years ago

Agreed the example was a bit unclean. I have posted a question on django-users and wait for a response before reopening it.

Meanwhile I have also created a standalone project to demonstrate the problem.

This version works:
https://github.com/iamrohitbanga/django_ticket_19271/tree/code_working

This version does not work:
https://github.com/iamrohitbanga/django_ticket_19271/tree/code_not_working

My concern is that it fails with the unique field only with the following stack trace:

"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 = '****'

comment:4 by Claude Paroz, 12 years ago

Thanks for the test code, it's a lot easier to debug.

Clearly, I don't understand why you are creating a ModelForm which is based on an abstract model. Even if this might work when no unique validation are made, it is by pure chance, in my opinion. If you need it to be dynamic, then also create dynamically the ModelForm instance (based on a non-abstract model).

comment:5 by iamrohitbanga@…, 12 years ago

So are you saying ModelForm with an abstract model is not supported? I can load it dynamically but it looks cleaner this way.

comment:6 by Claude Paroz, 12 years ago

Yes, I think so. If you think you have a use case for it, try to write on django-dev mailing list.

Note: See TracTickets for help on using tickets.
Back to Top