Search code examples
djangodjango-modelsdjango-viewsdjango-users

User OneToOneField in model


I would create a model to extend user profile, my model is this :

from django.db import models
from django.contrib.auth.models import User

class ExtendUser(models.Model):
    user = models.OneToOneField(User)
    #
    phone = models.CharField(max_length = 20)
    def __unicode__(self):
        return u"%s" % (self.phone)
    class Meta:
        verbose_name_plural = "ExtendUsers"

in the shell i try to extend an user profile in this way :

u = ExtendUser(user.id, phone)
u.save()

but obtain model empty, instead of user.id pass user obtain this error :

TypeError: int() argument must be a string or a number, not 'User' django


Solution

  • You should use keyword arguments when creating the instance.

    u = ExtendUser(user=user, phone=phone)
    

    Or, if you want to use the id instead of the user:

    u = ExtendUser(user_id=user.id, phone=phone)