Python论坛  - 讨论区

标题:[python-chinese] Django 表OneToOne关系问题。

2006年08月02日 星期三 03:58

lendcomcn lendcomcn at gmail.com
Wed Aug 2 03:58:55 HKT 2006

我建立了一个与系统auth_user表与Profile表OneToOne的关系,可是我测试在后台添加一个user后,并不会profile列出相应的ID来,大家帮忙看看,以下是Profile写的Models:

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

# Create your models here.
DEFAULT_DATE = datetime.now()

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

#------个人资料----------
truename = models.CharField('真实姓名',maxlength=50,blank=True)
sex = models.CharField('性别',choices=(('M','男'),('F','女')),maxlength=1,radio_admin=True,blank=True,default='F')
idcard = models.IntegerField('身份证',maxlength=20,blank=True)

class Admin:
list_display = ('user', 'truename', 'sex', 'idcard')

def __str__(self):
return self.user.username

请大家看看这是什么问题?

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年08月07日 星期一 11:00

憨狗 hackgou at 163.com
Mon Aug 7 11:00:06 HKT 2006

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Django官方的做法(也是最流行的做法)是在setting.py中添加
AUTH_PROFILE_MODULE = 'Profile.Profile'  #不是Profile.models.Profile
然后使用User的get_profile()来返回Profile.

但是按照默认的关系映射的方式来讲,直接使用每个Profile的实例对应对应的User
也是可以的。如同下面返回username那样:
def __str__(self):
 return self.user.username





lendcomcn wrote:
> 我建立了一个与系统auth_user表与Profile表OneToOne的关系,可是我测试在后台
> 添加一个user后,并不会profile列出相应的ID来,大家帮忙看看,以下是Profile
> 写的Models:
> 
> from django.db import models
> from django.contrib.auth.models import User
> from datetime import datetime
> 
> # Create your models here.
> DEFAULT_DATE = datetime.now()
> 
> class Profile(models.Model):
> user = models.OneToOneField(User)
> 
> #------个人资料----------
> truename = models.CharField('真实姓名',maxlength=50,blank=True)
> sex = models.CharField('性别',choices=(('M','男'),('F','女')),maxlength=
> 1,radio_admin=True,blank=True,default='F')
> idcard = models.IntegerField('身份证',maxlength=20,blank=True)
> 
> class Admin:
> list_display = ('user', 'truename', 'sex', 'idcard')
> 
> def __str__(self):
> return self.user.username
> 
> 请大家看看这是什么问题?
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.4 (MingW32)

iQIVAwUBRNastSZCCAzPcJ57AQLYVBAAkOCVyRHFI89i6N3+oGVEVi5YSiGprHyd
b4mVLkAle+VPIk4I/MrOVI9rh0XFf80YfUxngsiSXbshHyreZtWypveYKSeQcCAQ
2T+lqIECT4RydrZ3Bc3JZnK2aCMuwagPRk+DMaTDD3fxInGALdl8q9rZehZMC5NF
XiImn8p5XG8BgK/SEWFIjNvvEjmGzO7MHMxdrbWV4VsXUTbGnzqaYY+dLcf5MpGR
V4dM74g3OvrPyHG9PQtt/7waNqs+92TAxZwrbWEVWSb7njHV4GvHQqq6aZxEKlAd
DItlNqq2zmKhLxMKukZbbJvfSFQOpEG+8zLgDNPEMeehhBjaZQTKELoC3T1wczmW
Yya0Nkvs8Rtfb3oHEpBZuNWPP7j8Z9tCzk5PuqSV9YHfvKAKapiouUdvtnqYS9DA
xaeeZ6eqn6O9cyYw+71VxvGiJ24c+2FQSGK2eWHHHnn2Vk7rn9yRh8GM8MDuQREe
bsBpSum+AxTMCA3padLYrcZ/RssKMeX8GLwJCiPYKLNg6lzYuzH9k15zSxAASdNT
WZEFhr0QdAhU582U0IZGGgeZOUeGpndXONtAilnMjsaoJrPtpMgrGjppELys6/B2
HR/JEGyxaxqCV/kvgu3Hb/cdtvWtOyzq2o32zNRNg5k1/iKZILL0x3dRM2d7f9hA
PuZ0aEKBIe4=
=NONZ
-----END PGP SIGNATURE-----



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号