Reputation: 1617
I am getting this error
Cannot assign "<QuerySet [<UserProfile: UserProfile object (131)>]>": "BlogComment.userprofile" must be a "UserProfile" instance.
when saving my form.
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 foreign key named userprofile
so I am trying to save this instance like this:
isinstance.userprofile = user_profile
Where am I going wrong? What is my mistake?
I'm using this call for getting current user profile:
user_profile = UserProfile.objects.filter(user=request.user)
Upvotes: 0
Views: 34
Reputation: 6378
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)
Upvotes: 1