q = models.UserInfo.objects.all() select * from userinfo select * from userinfo inner join usertype on ... for row in q: print(row.name,row.ut.title) select_related: 查询主动做连表 q = models.UserInfo.objects.all().select_related('ut','gp') select * from us…
准备 定义如下模型 from django.db import models # 省份 class Province(models.Model): name = models.CharField(max_length=10) # 城市 class City(models.Model): name = models.CharField(max_length=5) province = models.ForeignKey(Province) # 人 class Person(models.Model…
models.py: from django.db import models class Employee2(models.Model): name = models.CharField(max_length=16) age = models.IntegerField() salary = models.IntegerField() province = models.CharField(max_length=32) dept = models.ForeignKey(to="Dept"…