extending django user model
I am extending the User object in django and have a user profile. So using the post_save hook I save a user profile in the following way:
class UserProfile(models.Model):
user = models.OneToOneField(User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
userObj, createdUser = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
It works fine when I run the shell, via python manage.py shell; saving a user object and user profile in the tables but if I test by launching a request, in the views.py that handles that request I do the following
def handleRequest(r开发者_如何学运维equest):
user = User(username="test",email="test",password="test")
user.save()
Of course I created a HttpResponse and returned a response as well but omitted it for brevity. The results are i get a User entry saved in auth_user but no user profile. What the dealy? Please help.
can not access the user profile, because you have in the user profile is a relationship.
you need add fields.
class UserProfile(models.Model):
url = models.URLField()
home_address = models.TextField()
phone_numer = models.PhoneNumberField()
user = models.ForeignKey(User, unique=True)
for more information check the following link
http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/
精彩评论