开发者

Django GenericForeignKey in the admin

class Comment(models.Model):
    text = models.TextField()
    timestamp = models.DateTimeField(auto_now_add = True)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

class Product(models.Model):
    name = models.CharField(max_length = 40)
    comments = generic.GenericRelation(Comment)

    def __unicode__(self):
        return self.name

In the Django admin I would if开发者_如何学Go possibile, under the "Comments" page, see the __unicode__ of the content object, for example can be Product.

Something this:

All comments

Comment 1 - to a Product - Foo Bar (unicode of Product) - timestamp

Comment 2 - to a UserProfile - Foo Bar (unicode of UserProfile) - timestamp

etc.

Ideas for admin.py?


I suggest adding unicode method to Comment model:

def __unicode__(self):
    return 'Comment %s - to  a %s - %s' % (self.pk, self.content_type, self.content_object.__unicode__(), self.timestamp)

If you are using standart ModelAdmin, then there is no need to change admin.py.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜