访问ManyToMany父对象

时间:2016-05-10 04:17:28

标签: python django

我有这两个类:

class Part(models.Model):
    title     = models.CharField(max_length=64)
    content   = models.TextField()

    def __str__(self):
        return self.title

class Review(models.Model):
    title        = models.CharField(max_length=64)
    parts        = models.ManyToManyField(Part)

    def __str__(self):
        return self.title

在部分管理员中,我想在list_display中显示评论标题。我试过这个:

class PartAdmin(admin.ModelAdmin):
    list_display   = ("title", "get_review")

    def get_review(self, obj):
        return obj.review_set.title
    get_review.admin_order_field = "part__review_set__title"
    get_review.short_description = "Review title"

admin.site.register(Part, PartAdmin)

我以为我可以通过review_set访问对象,但我遇到了一个AttributeError:

'ManyRelatedManager' object has no attribute 'title'

如何访问父对象以在list_display中显示它?

0 个答案:

没有答案