我正在尝试为旧数据库定义模型。 数据库架构:
CREATE TABLE sysuser
(
id serial NOT NULL,
login character varying(20) NOT NULL,
passwd character varying(80) NOT NULL,
created boolean DEFAULT false,
deleted boolean DEFAULT false,
CONSTRAINT sysuser_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
CREATE TABLE userinfo
(
id integer NOT NULL DEFAULT currval('sysuser_id_seq'::regclass),
name character varying(256),
surname character varying(256)
)
WITH (
OIDS=FALSE
);
使用id列的表之间存在一对一的关系。
我的目标是通过提供登录来获取姓名和用户名。 在SQL中,这将是: SELECT name,surname FROM sysuser JOIN userinfo USING(id)WHERE login =' joe1'
如何定义模型以允许我进行此类查询:
userlogin = 'joe1'
sysuser = Sysuser.objects.filter(deleted=False, created=True, login=userlogin).all()[:1]
#how to acheive this part?
print sysuser.userinfo.name # Joe
答案 0 :(得分:1)
我所要做的就是在db_column
模型中定义Sysuser
,如下所示:
userinfo = models.ForeignKey(Userinfo, db_column='id')