models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_img = models.ImageField(upload_to='ProfileImg', default='ProfileImg/default-avatar.png')
feedback = models.CharField(max_length=100, null = True)
views.py
While using objects.get()
user = User.objects.get(id=request.user.id)
user.userprofile.feedback = sentiment_Value
user.save()
it is working fine.
but while using objects.filter()
user = User.objects.filter(id=request.user.id)
user.userprofile.feedback = sentiment_Value
user.save()
I'm getting error
user.userprofile.feedback = sentiment_Value
AttributeError: 'QuerySet' object has no attribute 'userprofile'
and the user
returns
user: <QuerySet [<User: snegi8005>]>
I don't know why this is happening. I thought both get() and filter() function work same the only difference is get() raises exception if nothing found and filter() return none if no match found.
how get() and filter() works?
Please correct me if i'm wrong and help me solving this.
i want to get the user's data of a user.id and the associated table userprofile
data also. in case data not found it should return none or should not raise exception.
I know i can use get() with try/except block but that would be too much of code for me.