Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

python - How can I list all foreign key related objects in Django admin panel?

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:

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.R.R. Tolkien (books)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are looking for a 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:

 # 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:

# 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...