[Django]-(Django) Cannot assign "u'1'": "StaffProfile.user" must be a "User" instance

35👍

You need to assign a User object e.g.

from django.contrib.auth.models import User
user = User.objects.get(id=user_id)

staffprofile.user = user
👤JamesO

5👍

user needs to be an instance of the User model, not a unicode object (which is what you are passing it).

0👍

Yes you have to pass User instance in staffprofile.user = user_id user id place.

As @david-s pointed out in a comment, if you don’t have a user instance, you have to fetch from DB with an additional query.

Instead you can directly do is
staffprofile.user_id = user_id because Django behind the scene append _id in table for foreign keys so staffprofile.user will end staffprofile.user_id

Leave a comment