2011-07-17 52 views
0
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 

在Django管理我想如果不可能性,“注释”页面下,看到的内容对象的__unicode__,例如可以是产品。Django的GenericForeignKey在管理

东西这样的:

所有评论

注释1 - 一个产品 - 美孚酒吧(产品的统一) - 时间戳

评论2 - 为用户配置 - 美孚酒吧(unicode UserProfile) - 时间戳

想法admin.py

回答

1

我建议增加的unicode方法评论型号:

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

如果使用的是非标准的ModelAdmin,那么就没有必要改变admin.py。

+0

感谢您的回复。如何在模型管理页面的字段中显示__unicode__方法? –

+1

你的意思是使用list_display ?:'list_display('__ unicode __',)' – Pill

+0

你摇滚!非常感谢你! –