#19271: Form validation throws AttributeError has "Base Model has no attribute _default_manager" -------------------------------------+------------------------------------- Reporter: iamrohitbanga@… | Owner: nobody Type: Bug | Status: new Component: Forms | Version: 1.4 Severity: Normal | Resolution: Keywords: forms, models, | Triage Stage: unique constrait | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by claudep):
* needs_docs: => 0 * needs_tests: => 0 * needs_better_patch: => 0 Old description: > 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. > > {{{ > #!div style="font-size: 80%" > 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 New description: 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 -- Comment: Reformatted, please use preview before posting. -- Ticket URL: <https://code.djangoproject.com/ticket/19271#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.