如何在Django管理面板中列出所有与外键相关的对象?

时间:2021-04-03 22:55:05

I would like to implement a very simple feature with the django admin panel but I couldn't find the right way to achieve this so far:

我想用django管理面板实现一个非常简单的功能,但到目前为止我找不到正确的方法:

model.py

class Author(models.Model):
    name = models.CharField()

class Books(models.Model):
    title = models.CharField()
    author = models.ForeignKey(Author)

admin.py

class AuthorAdmin(admin.ModelAdmin):
    pass
admin.site.register(Author, AuthorAdmin)

How can I add a hyperlink to every item (Author) in the authors' list overview that links to a view showing all books of the specific author? For Example:

如何在作者列表概述中为每个项目(作者)添加一个超链接,该链接指向显示特定作者所有书籍的视图?例如:

  • J.K. Rowling (books)
  • J.K.罗琳(书)

  • J.R.R. Tolkien (books)
  • J.R.R.托尔金(书籍)

where books is a hyperlink to a site showing all books of the author.

书籍是指向显示作者所有书籍的网站的超链接。

3 个解决方案

#1


7  

You are looking for a ModelAdmin.list_filter.

您正在寻找ModelAdmin.list_filter。

Set list_filter to activate filters in the right sidebar of the change list page of the admin. A listfilter can be a field name, where the specified field should be either a BooleanField, CharField, DateField, DateTimeField, IntegerField, ForeignKey or ManyToManyField, for example:

设置list_filter以激活管理员更改列表页面右侧边栏中的过滤器。 listfilter可以是字段名,其中指定的字段应该是BooleanField,CharField,DateField,DateTimeField,IntegerField,ForeignKey或ManyToManyField,例如:

 # Add a list filter author to BookAdmin.
 # Now you can filter books by author.
 class BookAdmin(ModelAdmin):
    list_filter = ('author', )

Now you can use @Wolph suggestion to add a link in the Author list_display. This link points to the book list filtered by author:

现在,您可以使用@Wolph建议在作者list_display中添加链接。此链接指向由作者过滤的图书清单:

# Add hyperlinks to AuthorAdmin.
# Now you can jump to the book list filtered by autor. 
class AuthorAdmin(admin.ModelAdmin):
    def authors(self):
        return '<a href="/admin/appname/book/?author__id__exact=%d">%s</a>' % (self.author_id, self.author)
    authors.allow_tags = True

ALTERNATIVE. To save a click you can also point to the change view of a book directly:

替代方案。要保存点击,您还可以直接指向图书的更改视图:

class Books(models.Model):
    title = models.CharField()
    author = models.ForeignKey(Author)

def get_admin_url(self):
    return "/admin/appname/books/%d/" %self.id


class BookAdmin(admin.ModelAdmin):
    def authors(self):
        html = ""
        for obj in Books.objects.filter(author__id_exact=self.id):
            html += '<p><a href="%s">%s</a></p>' %(obj.get_admin_url(), obj.title)
        return html
    authors.allow_tags = True

    list_display = ['title', authors]

Disclaimer: Not tested, but if you fix the typo's it'll work! :)

免责声明:未经测试,但如果您修复错字,它将起作用! :)

Note that these links can also be injected at other points in the admin. When you add it to a widget, you can go from change view to change view.

请注意,这些链接也可以在管理员的其他位置注入。将其添加到窗口小部件后,您可以从更改视图转到更改视图。

#2


4  

Look into implementing an InlineModelAdmin object.

研究实现InlineModelAdmin对象。

# admin.py
class BooksInline(admin.TabularInline):
    model = Books

class AuthorAdmin(admin.ModelAdmin):
    inlines = [BooksInline]

Edited answer in response to OP's comment:

根据OP的评论编辑回答:

class AuthorAdmin(admin.ModelAdmin):
    list_display = ['name', 'books_published']

    def books_published(self, obj):
        redirect_url = reverse('admin:books_changelist')
        extra = "?author__id__exact=%d" % (obj.id)
        return "<a href='%s'>Books by author</a>" % (redirect_url + extra)

    books_published.allow_tags = True

#3


2  

It's not as simple as you might think actually, possible but not trivial since there is not that much documentation of this feature :)

它并不像你想象的那么简单,可能但不是微不足道的,因为没有那么多关于这个功能的文档:)

What you need is a custom column which is generated by a function which gives you a link to the Django admin with a given filter. Something like this (from the top of my head so not tested) should do the trick:

你需要的是一个自定义列,它由一个函数生成,它给你一个带给定过滤器的Django管理员的链接。像这样的东西(从我的头顶,所以没有测试)应该做的伎俩:

class AuthorAdmin(admin.ModelAdmin):
    def authors(self):
        return '<a href="?author=%d">%s</a>' % (self.author_id, self.author)
    authors.allow_html = True

    list_display = ['title', authors]

#1


7  

You are looking for a ModelAdmin.list_filter.

您正在寻找ModelAdmin.list_filter。

Set list_filter to activate filters in the right sidebar of the change list page of the admin. A listfilter can be a field name, where the specified field should be either a BooleanField, CharField, DateField, DateTimeField, IntegerField, ForeignKey or ManyToManyField, for example:

设置list_filter以激活管理员更改列表页面右侧边栏中的过滤器。 listfilter可以是字段名,其中指定的字段应该是BooleanField,CharField,DateField,DateTimeField,IntegerField,ForeignKey或ManyToManyField,例如:

 # Add a list filter author to BookAdmin.
 # Now you can filter books by author.
 class BookAdmin(ModelAdmin):
    list_filter = ('author', )

Now you can use @Wolph suggestion to add a link in the Author list_display. This link points to the book list filtered by author:

现在,您可以使用@Wolph建议在作者list_display中添加链接。此链接指向由作者过滤的图书清单:

# Add hyperlinks to AuthorAdmin.
# Now you can jump to the book list filtered by autor. 
class AuthorAdmin(admin.ModelAdmin):
    def authors(self):
        return '<a href="/admin/appname/book/?author__id__exact=%d">%s</a>' % (self.author_id, self.author)
    authors.allow_tags = True

ALTERNATIVE. To save a click you can also point to the change view of a book directly:

替代方案。要保存点击,您还可以直接指向图书的更改视图:

class Books(models.Model):
    title = models.CharField()
    author = models.ForeignKey(Author)

def get_admin_url(self):
    return "/admin/appname/books/%d/" %self.id


class BookAdmin(admin.ModelAdmin):
    def authors(self):
        html = ""
        for obj in Books.objects.filter(author__id_exact=self.id):
            html += '<p><a href="%s">%s</a></p>' %(obj.get_admin_url(), obj.title)
        return html
    authors.allow_tags = True

    list_display = ['title', authors]

Disclaimer: Not tested, but if you fix the typo's it'll work! :)

免责声明:未经测试,但如果您修复错字,它将起作用! :)

Note that these links can also be injected at other points in the admin. When you add it to a widget, you can go from change view to change view.

请注意,这些链接也可以在管理员的其他位置注入。将其添加到窗口小部件后,您可以从更改视图转到更改视图。

#2


4  

Look into implementing an InlineModelAdmin object.

研究实现InlineModelAdmin对象。

# admin.py
class BooksInline(admin.TabularInline):
    model = Books

class AuthorAdmin(admin.ModelAdmin):
    inlines = [BooksInline]

Edited answer in response to OP's comment:

根据OP的评论编辑回答:

class AuthorAdmin(admin.ModelAdmin):
    list_display = ['name', 'books_published']

    def books_published(self, obj):
        redirect_url = reverse('admin:books_changelist')
        extra = "?author__id__exact=%d" % (obj.id)
        return "<a href='%s'>Books by author</a>" % (redirect_url + extra)

    books_published.allow_tags = True

#3


2  

It's not as simple as you might think actually, possible but not trivial since there is not that much documentation of this feature :)

它并不像你想象的那么简单,可能但不是微不足道的,因为没有那么多关于这个功能的文档:)

What you need is a custom column which is generated by a function which gives you a link to the Django admin with a given filter. Something like this (from the top of my head so not tested) should do the trick:

你需要的是一个自定义列,它由一个函数生成,它给你一个带给定过滤器的Django管理员的链接。像这样的东西(从我的头顶,所以没有测试)应该做的伎俩:

class AuthorAdmin(admin.ModelAdmin):
    def authors(self):
        return '<a href="?author=%d">%s</a>' % (self.author_id, self.author)
    authors.allow_html = True

    list_display = ['title', authors]