Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

python - Django Admin Stack Inline Itself (Same Model) Indefinitely

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:

  1. User adds a File
  2. After adding a File, the user can either directly add a Segment or a Loop
  3. If the user directly adds Segment, well it ends there.
  4. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...