Opened 12 years ago
Last modified 9 years ago
#19255 assigned Bug
BaseGenericInlineFormSet runs validation methods before linking form instances to their related object
Reported by: | Bouke Haarsma | Owned by: | Ariel Pontes |
---|---|---|---|
Component: | contrib.contenttypes | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Given this model:
class Model(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() object = generic.GenericForeignKey() def clean(self): assert self.object_id assert self.object
and this admin:
class ModelInline(generic.GenericStackedInline): model = Model
When saving a new Model
, the assertion will fail. The object will not be linked, nor will the object_id be available to the clean function. This is because the fields are excluded from the formset and the values are set through the generic InlineAdmin, see this excerpt:
generic.py:412 def save_new(self, form, commit=True): # Avoid a circular import. from django.contrib.contenttypes.models import ContentType kwargs = { self.ct_field.get_attname(): ContentType.objects.get_for_model(self.instance).pk, self.ct_fk_field.get_attname(): self.instance.pk, } new_obj = self.model(**kwargs) return save_instance(form, new_obj, commit=commit)
To hack around this issue, the forms and formsets involved should not cache the validation result. The clean function should then validate the value if it is present (the second time when cleaning). The hack is quite ugly and requires a lot of coding. It should be possible to easily validate the newly related-to foreign key object.
Change History (5)
comment:1 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 12 years ago
Type: | Uncategorized → Bug |
---|
comment:3 by , 9 years ago
Summary: | Cannot validate generic foreignkey in new instance → BaseGenericInlineFormSet runs validation methods before linking form instances to their related object |
---|
See #25488 for a duplicate which proposes a solution.
comment:4 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 9 years ago
Update
Consider these models:
class Child(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() object = GenericForeignKey() def clean(self): assert self.object_id class Parent(models.Model): parent_number = models.IntegerField()
And these admins:
class ChildInline(GenericStackedInline): model = Child class ParentAdmin(admin.ModelAdmin): inlines = (ChildInline,) admin.site.register(Parent, ParentAdmin)
The easier part of the problem (#25488)
I started investigating this problem as described in #25488 and I came up with the following solution:
- Moving the linking from
save_new
to_construct_form
. It solves the problem described in that ticket, which reported a case in which there was a FormSet which was always initialized with a ParentModel instance.
This solution, however, breaks ANY attempt of saving a child inline in a "create parent" admin form. This happens because the FormSet is not initialized with a Parent instance in the admin, after all the parent doesn't exist yet (it's being created in the same form submission), which brings me to the revised solution:
- Doing the linking both in
save_new
AND_construct_form
. This way, if a form is instantiated with aninstance
parameter, theclean
will be able to access its instance's related object. It will only fail in the very particular case described above. It duplicates the linking but is still an improvement over the current behavior. It fixes the problem in #25488 without breaking anything (I ran the tests and they passed).
The harder part of the problem
Consider the following case:
- you have a Child model with a GenericRelation,
- validation in the Child's
clean
method that relies on the existence of the related object, - a Parent model,
- a ChildInline,
- a ParentAdmin with a ChildInline,
- and you want to create a new Parent record while at the same time saving a new Child record (in the same form submission)
Currently, this is pretty much impossible. Any solution I can think of entails some of the following serious compromises:
- We give up on the atomicity of the admin actions and save the parent record before validating the inlines. If there are errors, we show some (warning?) message like:
Your parent was created successfully but saving some of the children failed. Please correct the mistakes and re-submit the form to add them.
- We enforce the transactionality of this operation in the application layer, creating a parent record and deleting it if validation of the inlines fail. I have no idea how acceptable this is. Intuitively I'm not a big fan.
- We by default don't show inlines (inheriting from BaseInlineFormSet) in admin "new record" forms at all. It only appears once the parent is saved, in "edit record" forms.
Conclusion
Since I can't think of alternatives, in principle I'm going for the second solution. I'm thinking perhaps an improvement would be catching the django.db.models.fields.related.RelatedObjectDoesNotExist
Exception in BaseInlineFormSet.full_clean
and adding a more user-friendly message to the formset errors. Something along the lines of:
>>> formset.non_form_errors() ['Please make sure a parent record exists before trying to save a child.']
It might give the impression that this is never possible though, while in fact it's only not possible when there's a custom clean method trying to access the related object (the error would only be displayed in this situation).
Another idea would be throwing a more explanatory Django error, perhaps something like:
django.db.models.fields.related.RelatedObjectDoesNotExist: Cannot validate child's related object on parent creation
In any case, this solution wouldn't break any current code. The error would appear if there's a custom clean, otherwise the parent will be linked in save_new
as usual.
I'm able to reproduce the problem by adding an extra field into the Model class and creating another models which Model can point to:
Then I create a model admin for the other model:
When I create a new AnotherModel and try to save it I get this: