Opened 8 years ago

Last modified 5 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


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:
    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,
        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 Changed 8 years ago by matt@…

Triage Stage: UnreviewedAccepted

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:

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

class Model(models.Model):
    number = models.IntegerField()
    content_type = models.ForeignKey(ContentType, related_name='content')
    object_id = models.PositiveIntegerField()
    object = GenericForeignKey()

    def clean(self):
        assert self.object_id

class AnotherModel(models.Model):
    another_number = models.IntegerField()

Then I create a model admin for the other model:

from django.contrib.contenttypes.admin import GenericStackedInline
from django.contrib import admin

from .models import Model, AnotherModel

class ModelInline(GenericStackedInline):
    model = Model

class AnotherModelAdmin(admin.ModelAdmin):
    inlines = (ModelInline,), AnotherModelAdmin)

When I create a new AnotherModel and try to save it I get this:


Request Method: POST
Request URL:

Django Version: 1.6.dev20121122090508
Python Version: 2.7.2
Installed Applications:
Installed Middleware:

File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/core/handlers/" in get_response
  116.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/contrib/admin/" in wrapper
  369.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/utils/" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/views/decorators/" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/contrib/admin/" in inner
  202.             return view(request, *args, **kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/utils/" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/utils/" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/utils/" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/db/" in inner
  208.                 return func(*args, **kwargs)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/contrib/admin/" in add_view
  1035.             if all_valid(formsets) and form_validated:
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/forms/" in all_valid
  380.         if not formset.is_valid():
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/forms/" in is_valid
  277.         err = self.errors
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/forms/" in errors
  259.             self.full_clean()
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/forms/" in full_clean
  298.             self._errors.append(form.errors)
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/forms/" in _get_errors
  117.             self.full_clean()
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/forms/" in full_clean
  274.         self._post_clean()
File "/Users/matt/Code/envs/django/lib/python2.7/site-packages/django/forms/" in _post_clean
  333.             self.instance.clean()
File "/Users/matt/Code/envs/django/tickets/19255/testsite/testcase/" in clean
  12.         assert self.object_id

Exception Type: AssertionError at /admin/testcase/anothermodel/add/
Exception Value: 

Last edited 5 years ago by Tim Graham (previous) (diff)

comment:2 Changed 7 years ago by Aymeric Augustin

Type: UncategorizedBug

comment:3 Changed 5 years ago by Tim Graham

Summary: Cannot validate generic foreignkey in new instanceBaseGenericInlineFormSet runs validation methods before linking form instances to their related object

See #25488 for a duplicate which proposes a solution.

comment:4 Changed 5 years ago by Ariel Pontes

Owner: changed from nobody to Ariel Pontes
Status: newassigned

comment:5 Changed 5 years ago by Ariel Pontes


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,), 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:

  1. 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:

  1. Doing the linking both in save_new AND _construct_form. This way, if a form is instantiated with an instance parameter, the clean 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:

  1. 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.
  1. 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.
  1. 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.


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.

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