Skip to content

Instantly share code, notes, and snippets.

@tobyl
Last active July 19, 2024 13:51
Show Gist options
  • Save tobyl/deef5780a69a38f14884aaec86cbfab3 to your computer and use it in GitHub Desktop.
Save tobyl/deef5780a69a38f14884aaec86cbfab3 to your computer and use it in GitHub Desktop.
Nested inlines example
# models
class Part(models.Model):
title = models.CharField(max_length=255)
class PartAttribute(models.Model):
part = models.ForeignKey(Part, on_delete=models.CASCADE, null=True, related_name='attributes')
name = models.CharField(max_length=128)
class PartAttributeChoice(models.Model):
part = models.ForeignKey(Part, on_delete=models.CASCADE, null=True, related_name='choices')
part_attribute = models.ForeignKey(PartAttribute, on_delete=models.CASCADE, null=True, related_name='attributes')
name = models.CharField(max_length=128)
# admin
class PartAttributeChoiceInline(StackedInline):
model = PartAttributeChoice
fields = ('name',)
class PartAttributeInline(StackedInline):
model = PartAttribute
fields = ('name',)
inlines = (PartAttributeChoiceInline,)
@admin.register(Part, site=formula_admin_site)
class PartModelAdmin(ModelAdmin):
model = Part
fields = ('title',)
inlines = (PartAttributeInline, PartAttributeChoiceInline,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment