(Django) Cannot assign “u'1'”: “StaffProfile.user” must be a “User” instance

前端 未结 2 375
情话喂你
情话喂你 2020-12-14 18:09

I have a model like below:

class StaffProfile(models.Model):
    user = models.ForeignKey(User)
    maas = models.FloatField()
    maas_gunu = models.CharFie         


        
相关标签:
2条回答
  • 2020-12-14 18:56

    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
    
    0 讨论(0)
  • 2020-12-14 19:06

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

    0 讨论(0)
提交回复
热议问题