Opened 7 years ago

Closed 7 years ago

Last modified 7 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']
              return render_to_response("mytemplate.html", {'form' : form},
              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.

Change History (6)

comment:1 Changed 7 years ago by Claude Paroz

Description: modified (diff)

Reformatted, please use preview before posting.

comment:2 Changed 7 years ago by Claude Paroz

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 Changed 7 years ago by iamrohitbanga@…

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:

This version does not work:

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'

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "django_bug_19271/testproj/testproj/" in create_new
  33.         if form.is_valid():
File "/usr/local/lib/python2.7/dist-packages/django/forms/" in is_valid
  124.         return self.is_bound and not bool(self.errors)
File "/usr/local/lib/python2.7/dist-packages/django/forms/" in _get_errors
  115.             self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/" in full_clean
  272.         self._post_clean()
File "/usr/local/lib/python2.7/dist-packages/django/forms/" in _post_clean
  338.             self.validate_unique()
File "/usr/local/lib/python2.7/dist-packages/django/forms/" in validate_unique
  347.             self.instance.validate_unique(exclude=exclude)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/" in validate_unique
  633.         errors = self._perform_unique_checks(unique_checks)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/" 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

csrfmiddlewaretoken = u'**********'
my_id = u'1'
name = u'Rohit'


csrftoken = '****'

comment:4 Changed 7 years ago by Claude Paroz

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 Changed 7 years ago by iamrohitbanga@…

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 Changed 7 years ago by Claude Paroz

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