I am getting this error Cannot assign "<QuerySet [<UserProfile: UserProfile object (131)>]>": "BlogComment.userprofile" must be a "UserProfile" instance. during saving my forms. Here is my code:
if request.method == "POST":
if comment_form.is_valid():
isinstance = comment_form.save(commit=False)
name = request.POST['name']
email = request.POST['email']
if request.user.is_authenticated:
user_profile = UserProfile.objects.filter(user=request.user)
isinstance.blog = blog
isinstance.user = request.user
isinstance.name = name
isinstance.email = email
isinstance.userprofile = user_profile
isinstance.save()
models.py
class BlogComment(models.Model):
userprofile = models.ForeignKey(UserProfile,on_delete=models.CASCADE,null=True,blank=True)
#others fields....
In my models I have foreignkey named userprofile so I am trying to save this instance like this isinstance.userprofile = user_profile where I am doing mistake? using this queary user_profile = UserProfile.objects.filter(user=request.user) for getting current user profile
CodePudding user response:
This variable:
user_profile = UserProfile.objects.filter(user=request.user)
seems to be whole QuerySet. It has to be only one object. Like this:
user_profile = UserProfile.objects.get(user=request.user)
