无法分配“< SimpleLazyObject:< User:XXX>" ;:”Comment.user“必须是“MyProfile”例 [英] Cannot assign "<SimpleLazyObject: <User: XXX>>": "Comment.user" must be a "MyProfile" instance
问题描述
def post(request,slug):
user = get_object_or_404(User,username__iexact = request.user)
try:
profile = MyProfile.objects.get(user_id = request.user.id)
#如果是OneToOne字段,你可以:
#profile = request.user.myprofile
除了MyProfile.DoesNotExist:
profile = None
post = get_object_or_404(Post,slug = slug)
post.views + = 1#增加视图数
post.save()#并保存
comments = post.comment_set.all()
comment_form = CommentForm(request.POST或None)
如果comment_form.is_valid():
post_instance = comment_form.save(commit = False)
post_instance.user = request.user #this是发生错误的地方,如果我把request.user。 id simpleLazyObject dissapears。
post_instance.path = request.get_full_path()
post_instance.post = post
post_instance.save()
context_dict = {
'post':帖子,
'profile':个人资料,
'comments':评论,
'comment_form':comment_form
}
返回呈现(请求, main / post.html',context_dict)
我不知道comment.user是什么意思必须是myprofile实例。
在我的评论应用中,models.py我有
class评论(models.Model):
user = models.ForeignKey(MyProfile)
我的帐户应用程序,models.py我有
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField ,unique = True,verbose_name = _('user'),related_name ='my_profile')
不确定如何解决这个问题,任何帮助将被高度赞赏...
评论有ForeignKey到MyProfile,但是在触发错误的行中,您提供了一个用户模型。
正确的方式是:
my_p = MyProfile.objects.get(user = request.user)
post_instance.user = my_p
请注意,您使用:
MyProfile.objects.get(user = request.user)
而不是id字段。虽然幕后django使用id字段作为数据库中的真正的外键,但在代码中您可以使用该对象。关系字段是django执行关系查询的魔术的描述符。
Hello I know I have two problems here. one is simpleLazyObject issue which I can fix it with kinda hackish way. The other is "Comment.user" must be a "MyProfile" instance which I don't know how to fix. I think somewhere in the way, things got mixed up.
def post(request, slug):
user = get_object_or_404(User,username__iexact=request.user)
try:
profile = MyProfile.objects.get(user_id=request.user.id)
# if it's a OneToOne field, you can do:
# profile = request.user.myprofile
except MyProfile.DoesNotExist:
profile = None
post = get_object_or_404(Post, slug=slug)
post.views += 1 # increment the number of views
post.save() # and save it
comments = post.comment_set.all()
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid():
post_instance = comment_form.save(commit=False)
post_instance.user = request.user #this is where error is occuring, if I put request.user.id simpleLazyObject dissapears.
post_instance.path = request.get_full_path()
post_instance.post = post
post_instance.save()
context_dict = {
'post' :post,
'profile' :profile,
'comments':comments,
'comment_form': comment_form
}
return render(request, 'main/post.html', context_dict)
I'm not sure what it means by comment.user must be myprofile instance.
in my comments app, models.py I have
class Comment(models.Model):
user = models.ForeignKey(MyProfile)
and in my accounts app, models.py I have
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User, unique=True, verbose_name=_('user'), related_name='my_profile')
I'm not sure how to fix this problem, any help will be highly appreciated...
Comment has ForeignKey to MyProfile, yet in the line that triggers the error you provide a User model. The correct way would be:
my_p = MyProfile.objects.get(user=request.user)
post_instance.user = my_p
Note that you use:
MyProfile.objects.get(user=request.user)
And not the id field. Although behind the scenes django does use the id field as the real foreign key in the database, in your code you use the object. A relational field is a descriptor where django does the magic to run a relational query.
这篇关于无法分配“< SimpleLazyObject:< User:XXX>" ;:”Comment.user“必须是“MyProfile”例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!