BaseModelFormSet cannot resolve instances of inherited models when saving
|Reported by:||Bas Peschier||Owned by:||Brian Rosner|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
When you create a BaseModelFormSet for a Model which inherits from a user-defined Model, the save operation fails with a KeyError (key is None). This is because the primary key's attribute name is not what it expects it to be. As far as i can see the following is supposed to take place:
- creates a BaseModelFormSet, loads initials from model_to_dict for all forms from a QuerySet, including the 'id'-field
- ModelFormSet adds a hidden field via add_fields named pk.attname to each form which will contain the primary key. In normal (most?) cases, this attname is 'id'
- The form uses initial data when cleaning, outputing the pk nicely in the hidden field.
- When saving, use the hidden field to retrieve the object it refers to and save it.
The fact that the attributename of a pk for an inherited model is "<parentmodel>_ptr_id" screws things up. This can be fixed in three ways:
- Patch model_to_dict to include the actual pk.attrname: pk key/value-pair or...
- Setup the hiddenfield in add_fields of BaseModelFormSet to include a initial value
- Some clever way I did not think of yet.
I really love these formsets, they reduce coding time with large factors instead of reducing it by a few minutes :-)
Change History (15)
comment:1 Changed 8 years ago by
|milestone:||→ 1.0 beta|
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Accepted|
comment:10 Changed 8 years ago by
|Patch needs improvement:||set|
|Status:||closed → reopened|