Database example:
class Brand(models.Model):
brand_name = models.CharField(max_length=30
class Model(models.Model):
brand = models.ForeignKey(Brand)
model_name = models.CharField(max_length=30)
Now in admin
admin.site.register(Brand)
class ModelAdmin(admin.ModelAdmin):
list_display = ['brand', 'model_name']
fields = ['model_name', 'brand']
admin.site.register(Model, ModelAdmin)
How can I show in the BrandAdmin page all the models that are associated with the one brand? As it stands right now, the Brand page only shows the brands, it doesn't show any of the models associated with it.
Well, your best choice are django inlines
#admin.py
class ModelAdmin(admin.ModelAdmin):
list_display = ['brand', 'model_name']
fields = ['model_name', 'brand']
class ModelInline(admin.TabularInline):
model = Model
class BrandAdmin(admin.ModelAdmin):
model = Brand
inlines = [
ModelInline,
]
admin.site.register(Brand, BrandAdmin)
admin.site.register(Model, ModelAdmin)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With