2013-04-30 54 views
3

我有一個模型評論:ReverseSingleRelatedObjectDescriptor對象有沒有屬性「用戶名」

class Comment(models.Model): 
    text = models.CharField(max_length=140) 
    owner = models.ForeignKey(User, null=True, blank=True) 
    created = models.DateTimeField(default=datetime.datetime.utcnow) 

當我嘗試創建一個評論,我得到一個AttributeError:

ipdb> usr = User.objects.get(pk=1) 
ipdb> usr 
<User: admin> 
ipdb> Comment.objects.create(owner=usr) 
*** AttributeError: 'ReverseSingleRelatedObjectDescriptor' object has no attribute 'username' 
ipdb> usr.username 
u'admin' 
ipdb> 
+0

我現在沒有,如果它與它有關,但你不接受null在文本字段,所以它會創建無論如何失敗。 – 2013-04-30 15:55:31

+0

我tryed也owner = models.ForeignKey(用戶,null = True,空白=真)沒有成功 – kollo 2013-04-30 16:00:41

+0

我的意思是:'text = models.CharField(max_length = 140,null = True)' – 2013-04-30 16:10:13

回答

4

我有這個回溯,得:

'ReverseSingleRelatedObjectDescriptor' object has no attribute my_attribute 

我找到了解決辦法:

Comment.user # Access on class level: does raise the above error 

comment.user # Access on instance: this works! 
相關問題