django-nested-inlines
django-nested-inlines copied to clipboard
Support for generic inlines
Currently, mixing Nested(Stacked|Tabular)Admin with Generic(Stacked|Tabular)Admin is not working.
IMO, this is a critical feature.
Has anyone been able to find a good workaround for this issue?
Not me, unfortunately...
I have not tried out a generic nested inline, however I do use a generic inline with a NestedModelAdmin at the moment. I kept getting an error, but the solution was very simple. All I needed to do was add inlines = [] to the generic admin inline.
So the following will only work with a generic inline that is an end point (does not have inlines of it's own)! So this is not a true generic nested inline, but I'm able to help someone.
Say you have a model:
class A(models.Model):
pass
d_objects = generic.GenericRelation('D')
class B(models.Model):
parent = models.ForeignKey(A)
class C(models.Model):
parent = models.ForeignKey(B)
class D(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
So A is our root object. A has B as children which has C as children. For this argument D has only A as a foreign relation.
Admin.py:
from django.contrib import admin
from django.contrib.contenttypes import generic
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline
class C_Inline(NestedStackedInline):
model = C
class B_Inline(NestedStackedInline):
model = B
inlines = [C_Inline]
class D_Inline(generic.GenericStackedInline):
model = D
inlines = [] # THIS IS THE FIX
class A_Admin(NestedModelAdmin):
inlines = [B_inline, D_inline, ]
admin.site.register(A, A_Admin)
Possible all that is needed to get nested inlines to work with generic keys is adding: ct_field = "content_type" ct_fk_field = "object_id" To a "NestedStackedInline". But my setup doesn't require this, so I haven't tested it at the moment.
@BertrandBordage hey man, i just re-ran into this problem. I found a solution a while back: http://stackoverflow.com/questions/23909800/django-nested-inlines-not-shown-in-admin-site/23913043?noredirect=1#comment47190496_23913043
@brmc Hi! Thanks for sharing. Nice coincidence, I’m currently working on my own complete rewrite of inlines to handle limitless nested inlines: https://github.com/BertrandBordage/django-super-inlines. I got a first awesome result 5 minutes ago :) I also found a bunch of bugs in the inlines. It was so poorly coded…