django-nested-inline
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
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).
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?
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)
I am running into the same problem. Is there a solution or workaround for this?
:+1:
Nine months and there is not any solution yet?
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.
@wrvdklooster @GabKlein @jhrs21 see https://github.com/BertrandBordage/django-super-inlines
Running into the same problem now.
So this problem isn't solved? Im running the same problem "Parent object must be created when creating nested inlines"
Problem is still exists :(((
6 years and still counting :(
7 years and still counting :(
@nishantonline1 If you have a PR I'll happily take a look
Do you have reached a solution for this without using an unnecesary field in the levelOne? I only could solve this with that