ORM 外键查询
Django 的 ORM 有多种关系:一对一,多对一,多对多。
各自定义的方式为 :
一对一: OneToOneField
多对一: ForeignKey
多对多: ManyToManyField
from django.db import models class Engine(models.Model): name = models.CharField(max_length=25) def __str__(self): #2.x版本 __unicode__(self) return self.name class Car(models.Model): name = models.CharField(max_length=25) engine = models.OneToOneField(Engine) def __str__(self): return self.name class Engine2(models.Model): name = models.CharField(max_length=25) def __str__(self): return self.name class Car2(models.Model): name = models.CharField(max_length=25) engine = models.ForeignKey(Engine2, unique=True) def __str__(self): return self.name
OneToOneField
Example
from testapp.models import Car, Engine c = Car.objects.get(name='Audi') e = Engine.objects.get(name='Diesel') e.car # engine 的model 定义中并没有car,这个是自动生成的,用关联表的类名小写直接访问 <Car: Audi> # 注意返回内容的不同
ForeignKey
with unique=True
Example
from testapp.models import Car2, Engine2 c2 = Car2.objects.get(name='Mazda') e2 = Engine2.objects.get(name='Wankel') # 在未定义的model中用关联表类名小写加"_set"来访问,多对多也一样 e2.car2_set.all() # 注意返回内容的不同,这里是一个QuerySet [<Car2: Mazda>]
CODE VIEW
from django.db import models # Create your models here. class IDC(models.Model): """机房""" name = models.CharField(max_length=64, unique=True) class Meta: verbose_name = '机房' verbose_name_plural = "机房" def __str__(self): return self.name class Host(models.Model): hostname = models.CharField(u'主机名', max_length=64, default="localhost", blank=True, null=True) intranet_ipaddr = models.GenericIPAddressField(u'内网地址', unique=True) network_ipaddr = models.GenericIPAddressField(u'公网地址', null=True, default=None, blank=True) memo = models.TextField(u'备注', blank=True, null=True, default=None) status_type = ( (0, '离线'), (1, '在线'), (2, '维护中') ) status = models.SmallIntegerField(u'状态', choices=status_type, default=1) user = models.ManyToManyField('User', blank=True) idc = models.ForeignKey('IDC', blank=True, null=True) system_type_choices = ((0, 'Linux'), (1, 'Windows')) system_type = models.SmallIntegerField(choices=system_type_choices, default=0) enabled = models.BooleanField(default=1, verbose_name="启用本机") class Meta: verbose_name = '主机' verbose_name_plural = "主机" def __str__(self): return self.hostname class BusinessUnit(models.Model): name = models.CharField(u'业务线', max_length=64, unique=True) parent_unit = models.ForeignKey('self', null=True, blank=True) memo = models.CharField(u'备注', max_length=128, blank=True) class Meta: verbose_name = '业务线' verbose_name_plural = "业务线" def __str__(self): return self.name class User(models.Model): username = models.CharField(u'用户名', max_length=64, null=True, blank=True) passwd = models.CharField(u'密码', max_length=128, null=True, blank=True) class Meta: verbose_name = '用户表' verbose_name_plural = "用户表" unique_together = ("username", 'passwd') def __str__(self): return self.username
IDC表
from userauth import models i = models.IDC.objects.get(name="苏州桥机房") # 反查询,使用小写的表名加_set字段 i.host_set.all() # 查询结果为QuerySet类型 <QuerySet [<Host: localhost>, <Host: 56.2>]>
Host表
from userauth import models h = models.Host.objects.get(id=1) h.hostname ----------'localhost' h.intranet_ipaddr ----------'10.10.56.1' h.user.all() # 查询多对多字段 ---------<QuerySet [<User: root>, <User: tomcat>]> h.idc # 查询一对多字段 ----------<IDC: 苏州桥机房>
User表
from userauth import models u = models.User.objects.get(username='root') u.username ---------'root' u.passwd ---------'12345678' u.host_set.all() # 反查询多对多字段 --------<QuerySet [<Host: localhost>]>
BusinessUnit表
from userauth import models b = models.BusinessUnit.objects.all() b.name --------'北京' b.parent_unit # 自关联的一对多 -------------<BusinessUnit: 二手车平台>