2014-10-09 3 views
0

У меня ниже модель, и я пытаюсь эти две команды в django shell:UserProfile (user_id = 2) возвращает <UserProfile: пользователь>, но UserProfile (user_id = 2) .birth_year возвращает None

  1. from auth_lifecycle.models import UserProfile
  2. UserProfile(user_id=2).birth_year

Но это возвращение None, несмотря на UserProfile(user_id=2) возвращение <UserProfile: user>.

Вот запрос подтверждения данных существует:

auth_lifecycle_db=# select * from auth_lifecycle_userprofile; 
    id | birth_year | user_id 
----+------------+--------- 
    1 |  1905 |  1 
    2 |  1910 |  2 
(2 rows) 

Как получить доступ к атрибуту birth_year?


models.py

"""Defines a single extra user-profile field for the user-authentication 
    lifecycle demo project: Birth year 
""" 
from django.contrib.auth.models import User 
from django.db     import models 


class UserProfile(models.Model): 
    """Extra information about a user: Birth year and profile picture. See 
     the package doc for more info. 

     ---NOTES--- 

     Useful related SQL: 
      - `select id from auth_user where username <> 'admin';` 
      - `select * from auth_lifecycle_userprofile where user_id=(x,x,...);` 
    """ 
    # This line is required. Links UserProfile to a User model instance. 
    user = models.OneToOneField(User, related_name="profile") 

    # The additional attributes we wish to include. 
    birth_year = models.IntegerField(
     blank=True, 
     verbose_name="Year you were born") 

    # Override the __str__() method to return out something meaningful 
    def __str__(self): 
     return self.user.username 

ответ

Смежные вопросы