djangodjango-modelsdjango-admindjango-admin-filtersdjango-admin-tools

django admin site foreign key fields


Can we have foreign key model fields display in another model as fields (not list display) on django admin site. I have a product and price model which has foreignkey of Price. I would like to display price, sale_price and sale as editable fields in both Product and Variation model.

class Price(models.Model):
    price = models.DecimalField(decimal_places=2, max_digits=8)
    sale_price = models.DecimalField(decimal_places=2, max_digits=8)
    sale = models.BooleanField(default=False)

class Product(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField(blank=True, null=True)
    price =  models.ForeignKey('Price')

class Variation(models.Model):
    product = models.ForeignKey(Product)
    price =  models.ForeignKey('Price')
    title = models.CharField(max_length=120)

Solution

  • You can use the the admin inline. https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#inlinemodeladmin-objects