需要在django admin中为另一个模型添加视图列表锚标记

时间:2022-02-05 19:41:04

I have a two models question and answers. I am now able to show both the models in django admin. But i need to show a link for View Answers in question listing page in django admin. And this link should only show the answers of related question.

我有两个模型问答。我现在能够在django admin中显示这两个模型。但我需要在django admin中显示问题列表页面中的查看答案的链接。此链接应仅显示相关问题的答案。

For example

例如

Question  Date     ViewAnswers
A             21.2.12 View Answers
B             22.2.12 View Answers

问题日期ViewAnswers A 21.2.12查看答案B 22.2.12查看答案

Answer model have foreign key of question in it. And i want by clicking the view answers link i am able to see listing of all the answers for that question.

答案模型中有问题的外键。我想通过点击查看答案链接,我能够看到该问题的所有答案的列表。

1 个解决方案

#1


1  

I assume that your app is called app and question field of the Answer model is named question :-)

我假设您的应用程序被称为应用程序,答案模型的问题字段被命名为问题:-)

from django.core.urlresolvers import reverse

class QuestionAdmin(admin.ModelAdmin):
    list_display = ['question', 'date', 'view_answers']

    def view_answers(self, obj):
        url = reverse("admin:app_answer_changelist")
        return '<a href="%s?question=%d">View answers</a>' % (url, obj.pk)
    view_answers.short_description = 'View answers'
    view_answers.allow_tags = True

#1


1  

I assume that your app is called app and question field of the Answer model is named question :-)

我假设您的应用程序被称为应用程序,答案模型的问题字段被命名为问题:-)

from django.core.urlresolvers import reverse

class QuestionAdmin(admin.ModelAdmin):
    list_display = ['question', 'date', 'view_answers']

    def view_answers(self, obj):
        url = reverse("admin:app_answer_changelist")
        return '<a href="%s?question=%d">View answers</a>' % (url, obj.pk)
    view_answers.short_description = 'View answers'
    view_answers.allow_tags = True