django-nested-inline icon indicating copy to clipboard operation
django-nested-inline copied to clipboard

"Parent object must be created when creating nested inlines" error when parent object has no fields other than FK

Open smcoll opened this issue 10 years ago • 14 comments
trafficstars

i have an intermediate model which is little more than a logical partition, so it has no fields except for an FK. See this LevelOne, for example:

class TopLevel(models.Model):
    name = models.CharField(max_length=200)

class LevelOne(models.Model):
    level = models.ForeignKey('TopLevel')

class LevelTwo(models.Model):
    name = models.CharField(max_length=200)
    level = models.ForeignKey('LevelOne')

Then with an admin configuration like this:

class LevelTwoInline(NestedStackedInline):
    model = LevelTwo
    extra = 1
    fk_name = 'level'

class LevelOneInline(NestedStackedInline):
    model = LevelOne
    extra = 1
    fk_name = 'level'
    inlines = [LevelTwoInline]

class TopLevelAdmin(NestedModelAdmin):
    model = TopLevel
    inlines = [LevelOneInline]

admin.site.register(TopLevel, TopLevelAdmin)

This appears to be due to line 141 of nested_inline/admin.py:

if (not hasattr(form, 'cleaned_data') or not form.cleaned_data) and self.formset_has_nested_data(form.nested_formsets):

form.cleaned_data is {} in this case (there are no fields to clean, other than the fk).

smcoll avatar Nov 26 '14 17:11 smcoll

Help me understand the purpose of this particular check.

  • Line 129 makes sure each form in the formset is individually valid.
  • Line 133 exempts unbound forms.
  • Line 137 makes it recursive.

Then 141 makes sure that (if it has nested data) form.cleaned_data is not missing, and is not empty (this is the problematic check for my form), returning the error "Parent object must be created when creating nested inlines."

Why wouldn't the preceding checks be sufficient? Don't we already know (because the form is bound and is valid) that we have enough information to save this parent object? It seems that without this check, there is an IntegrityError because the parent object isn't created in that case. Can you explain how the logic of the save works, in that case?

smcoll avatar Dec 02 '14 00:12 smcoll

i also discovered that similar behavior occurs when the parent inline has only attributes which are blank=True (or null=True) and no values are submitted, since form.cleaned_data is still an empty dictionary in that case.

Same behavior if all fields are hidden, regardless of whether they have values.

Same behavior if the field has a default value which has not been changed (it doesn't register in form.cleaned_data)

smcoll avatar Dec 02 '14 15:12 smcoll

I am running into the same problem. Is there a solution or workaround for this?

wrvdklooster avatar Feb 15 '15 19:02 wrvdklooster

:+1:

GabKlein avatar Mar 18 '15 19:03 GabKlein

Nine months and there is not any solution yet?

jhrs21 avatar Aug 26 '15 15:08 jhrs21

Seems to happen only when you have a default value defined for a model.

One way to hack it is to try to set initial values for your forms for the relevant models and to remove the default's.

edinbajramovic avatar Sep 02 '15 05:09 edinbajramovic

@wrvdklooster @GabKlein @jhrs21 see https://github.com/BertrandBordage/django-super-inlines

smcoll avatar Nov 10 '15 20:11 smcoll

Running into the same problem now.

Adnn avatar Mar 12 '16 09:03 Adnn

So this problem isn't solved? Im running the same problem "Parent object must be created when creating nested inlines"

ghost avatar May 18 '16 20:05 ghost

Problem is still exists :(((

relonger avatar Jan 15 '17 13:01 relonger

6 years and still counting :(

onurmatik avatar May 24 '20 12:05 onurmatik

7 years and still counting :(

nishantonline1 avatar Aug 18 '21 08:08 nishantonline1

@nishantonline1 If you have a PR I'll happily take a look

OskarPersson avatar Aug 18 '21 08:08 OskarPersson

Do you have reached a solution for this without using an unnecesary field in the levelOne? I only could solve this with that

Extremoduro1234 avatar Nov 01 '23 18:11 Extremoduro1234