Django 1.3 Custom File Match Request Does Not Exist

I have a little problem with the user model, the model looks like this:

#! -*- coding: utf-8 -*-

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

class UserProfile(models.Model):
      url = models.URLField(max_length = 70, blank = True, verbose_name = 'WWW')
      home_address = models.TextField(blank = True, verbose_name = 'Home Adress')
      user = models.ForeignKey(User, blank = True, unique = True)

      def __unicode__(self):
          return '%s' %(self.user)

When I open django-shell and import the user first:

u = User.objects.get(id = 1)

and then:

zm = UserProfile.objects.get(user = u)

I get an error message:

DoNotExist: UserProfile matching request does not exist.

The idea is simple: first I create a user, it works, then I want to add some information to the user, it does not work: /

+5
source share
2 answers

Are you sure the UserProfile object for this user exists? Django does not automatically create it for you.

What you might want is this:

u = User.objects.get(id=1)
zm, created = UserProfile.objects.get_or_create(user = u)

, ( AUTH_PROFILE_MODULE), :

u = User.objects.get(id=1)
zm = u.get_profile()
+7

, Django . . - , , .

+3

All Articles