你好,我知道我这里有两个问题。一个是一个SimpleLazyObject
我可以用一种有点黑客的方式解决这个问题。另一个是“Comment.user
“必须是一个”MyProfile
“我不知道如何解决这个问题。我认为在某种程度上,事情变得混乱了。
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)
我不确定这是什么意思Comment.user
必须是MyProfile
实例。
在我的评论应用程序中,模型.py I have
class Comment(models.Model):
user = models.ForeignKey(MyProfile)
在我的帐户应用程序中,模型.py I have
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User, unique=True, verbose_name=_('user'), related_name='my_profile')
我不确定如何解决这个问题,任何帮助将不胜感激。