0

我有两个模型

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")

如何discount在管理面板中显示价值?

class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'category', 'price', 'discount'] # not works
    list_filter = ['category', 'price']
    list_editable = ['price']
4

1 回答 1

0

这是解决方案:

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
于 2018-01-28T21:37:13.547 回答