0

I have two models

class Category(models.Model):
    name = models.CharField("category", max_length=200, null=True)
    discount = models.PositiveIntegerField(null=True)

class Product(models.Model):
    name = models.CharField("product", max_length=200)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="products", verbose_name="category")

How to show discount value in admin panel?

class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'category', 'price', 'discount'] # not works
    list_filter = ['category', 'price']
    list_editable = ['price']
shmnff
  • 647
  • 2
  • 15
  • 31
  • 1
    Possible duplicate of [Can "list\_display" in a Django ModelAdmin display attributes of ForeignKey fields?](https://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-field) – Daniel Konovalenko Jan 28 '18 at 20:34

1 Answers1

0

Here is solution:

class Product(models.Model):
    name = models.CharField("product", max_length=200)
    price = models.DecimalField("price", max_digits=10, decimal_places=2)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="products", verbose_name="category")
    def discount_value(self):
        return self.category.discount
shmnff
  • 647
  • 2
  • 15
  • 31