Given the following models
class File(models.Model):
id = models.AutoField(primary_key=True)
class Loop(models.Model):
id = models.AutoField(primary_key=True)
file = models.ForeignKey(File,
on_delete=models.CASCADE, null=True, blank=True)
loop = models.ForeignKey("self",
on_delete=models.CASCADE, null=True, blank=True)
class Segment(models.Model):
id = models.AutoField(primary_key=True)
file = models.ForeignKey(File,
on_delete=models.CASCADE, null=True, blank=True)
loop = models.ForeignKey(Loop,
on_delete=models.CASCADE, null=True, blank=True)
I want to build a nested admin where the user can:
- User adds a File
- After adding a File, the user can either directly add a Segment or a Loop
- If the user directly adds Segment, well it ends there.
- However, if the user happens to add a Loop, he/she should have an option to again add either the Loop or Segment
Something like this:
File
Segment
Loop
Segment
Loop
Loop
Segment
Segment
.....
.....
I have tried the following but it apparently doesn't work.
class SegmentInline(nested_admin.NestedModelAdmin):
model = Segment
class LoopInline(nested_admin.NestedModelAdmin):
model = Loop
inlines = [SegmentInline, LoopInline]
class FileAdmin(nested_admin.NestedModelAdmin):
model = File
inlines = [SegmentInline, LoopInline]
It fails with the following error:
NameError: name 'LoopInline' is not defined
How do I achieve this in Django Admin?
Any workarounds to this?
question from:
https://stackoverflow.com/questions/66055751/django-admin-stack-inline-itself-same-model-indefinitely 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…