[Django]-Cannot assign "<SimpleLazyObject: <User: XXX>>": "Comment.user" must be a "MyProfile" instance

6👍

✅

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.

Leave a comment