Given a model named MainModel
and a RelatedModel
, where the later has a ForeignKey
field to MainModel
:
class MainModel(models.Model):
name = models.CharField(max_length=50)
type = models.BooleanField()
class RelatedModel1(models.Model):
main = models.ForeingKey(MainModel):
name = models.CharField(max_length=50)
class RelatedModel2(models.Model):
main = models.ForeingKey(MainModel):
name = models.CharField(max_length=50)
and the corresponding ModelAdmin classes:
class RelatedModel1InlineAdmin(admin.TabularInline):
model = RelatedModel1
class RelatedModel2InlineAdmin(admin.TabularInline):
model = RelatedModel2
class MainModel(admin.ModelAdmin):
inlines = [RelatedModel1, RelatedModel2]
And that's the default behavior, you get two inlines, one for every related model. The question is how to hide completely all the inlines when the MainModel
instance is being created (the ModelAdmin
's add_view
), and to show the inlines for RelatedModel1
when the type
field of the MainModel
instance is True
, and show the inlines for RelatedModel2
when False
.
I was going to create a descriptor for the ModelAdmin.inline_instances
attribute, but I realized that I need access to the object instance being edited, but it is passed around as parameters.
Any help?
Thanks!