Opened 12 years ago
Last modified 12 years ago
#19271 closed Bug
Form validation throws AttributeError has "Base Model has no attribute _default_manager" — at Initial Version
Reported by: | 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
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_datafield1 mymodel.field2 = form.cleaned_datafield3 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