TotuDoum
TotuDoum

Reputation: 137

how to update OneToOneField of django'user

I'm using the real django user.
In my project my user can create multiple warriors.
When I want to play I have to choose one of it.

So here is what my class looks like:

class Warrior:
    nom = models.CharField(max_length=30, unique=True)
    ...

class Profile(models.Model):
    user = models.OneToOneField(User)
    warrior = models.ForeignKey(Warrior)

in my view, I want to change the actual warrior I'm playing with:

warrior = get_object_or_404(Warrior, id=id)
request.user.profile.warrior = warrior

But that code doesn't work. I think I have an error somewhere .

Upvotes: 0

Views: 1475

Answers (1)

catavaran
catavaran

Reputation: 45575

request.user.profile.warrior = warrior
request.user.profile.save()

Upvotes: 2

Related Questions