I'm trying to define models for a legacy database. Database schema:
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
);
There is one to one relationship between those to tables using id column.
My goal is to get name and username by providing a login. In SQL this would be: SELECT name, surname FROM sysuser JOIN userinfo USING(id) WHERE login = 'joe1'
How can I define my models to allow me to make this kind of query:
userlogin = 'joe1'
sysuser = Sysuser.objects.filter(deleted=False, created=True, login=userlogin).all()[:1]
#how to acheive this part?
print sysuser.userinfo.name # Joe
我要做的就是在Sysuser
模型中定义db_column
,如下所示:
userinfo = models.ForeignKey(Userinfo, db_column='id')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.