Opened 11 years ago

Last modified 11 years ago

#19271 closed Bug

Form validation throws AttributeError has "Base Model has no attribute _default_manager" — at Version 1

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 (1)

comment:1 by Claude Paroz, 11 years ago

Description: modified (diff)

Reformatted, please use preview before posting.

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