django admin模型属性中的自定义视图

时间:2016-09-21 13:53:11

标签: python django django-models django-admin django-views

我有这样的模特:

class Product(models.Model):
    ...
    id = models.IntegerField(unique=True)
    ...

class Question(models.Model):
    product = models.ForeignKey(Product, related_name='question', null=True)
    answer = models.ForeignKey(Answer, related_name='question', blank=True, null=True)
    user = models.ForeignKey(User, null=True)
    text = models.TextField(null=True)
    ...

class Answer(models.Model):
    user = models.ForeignKey(User, null=True)
    text = models.TextField(null=True)
    ...

所有这些型号都在django admin中注册。 如何在编辑其中一个问题时获得自定义报告表(/ admin / qa / question / 1 / change /):

...
editable standart_fields from Question model
...
non-standart report(without editable fields):
all questions:                   related answers to them
User: Question(related to a product) - User: Answer to it
User: Question(if it exists)         - User: answer to it 

管理网站是否可以?

1 个答案:

答案 0 :(得分:1)

您需要为问题创建自定义 ModelAdmin 并覆盖表单属性,如{{3}中所述}。

在您的情况下,您应该使用 ModelAdmin.get_form()获取动态创建的表单,并使用Django的表单框架添加您想要的报表。