我在django中有以下代码 型号:
class Book(models.Model):
book_id = models.CharField(max_length=10, primary_key=True)
title = models.CharField(max_length=255)
class Author(models.Model):
books = models.ForeignKey(Book)
author_name = models.CharField(max_length=200)
search_fields=['author_name']
class BookAdmin(admin.ModelAdmin):
model = Book
list_display=['book_id', 'title', 'get_author']
search_fields = ['title', 'book_id']
def get_author(self, obj):
names = [a.author_name for a in obj.author_set.all()]
return names
是否有其他方法可以在图书管理页面中显示作者列表。因为这个结果是以unicode输出 作者姓名 [u'Zev Halevi'] [u'Kathryn Worth',u'Dorothy Bayley']
另外,我需要提供一个单独的搜索栏来搜索作者。我无法使用author_name列,因为这是一个外键
答案 0 :(得分:0)
使用此
def get_author(self, obj):
names = "\n".join([a.author_name for a in obj.author_set.all()])
return names