一对多
class UserType(models.Model): caption = models.CharField(max_length=32) class UserInfo(models.Model): user_type = models.ForeignKey(UserType)# user_type对象中封装id,caption username = models.CharField(max_length=32) age = models.IntegerField()
增:
1.外键id添加
models.UserInfo.objects.create(username='xs',age=19,user_type_id=1)
2.直接添加外键的对象
obj = models.UserType(caption='haha') obj.save() models.UserInfo.objects.create(username='xs',age=18,user_type=obj)
查:
正向查询:根据userinfo查usertype
result = models.UserInfo.objects.filter(user_type__caption='CTO') for item in result: print item.username,item.age,item.user_type.caption
反向查询:根据usertype查userinfo
result = models.UserType.objects.get(id=1) print '-->0',result print '-->1',result.userinfo_set print '-->2',result.userinfo_set.all() print '-->3',result.userinfo_set.filter(username='xs') #用字段条件查询 print '-->4',models.UserInfo.objects.filter(user_type=result) #用对象条件查询 user_type_obj = models.UserType.objects.get(userinfo__username='xs') print '-->0',user_type_obj.caption print '-->1',user_type_obj.userinfo_set.all().count() return HttpResponse('ok')
多对多
class Host(models.Model): hostname = models.CharField(max_length=32) port = models.IntegerField() class HostAdmin(models.Model): username = models.CharField(max_length=32) email = models.CharField(max_length=32) host = models.ManyToManyField(Host)
增:
正向增:
admin_obj = models.HostAdmin.objects.get(username='xs') host_list = models.Host.objects.filter(id__lt=3) admin_obj.host.add(*host_list)
反向增:
host_obj = models.Host.objects.get(id=3) admin_list= models.HostAdmin.objects.filter(id__gt=1) host_obj.hostadmin_set.add(*admin_list)
区别总结:区别在于正向查拥有自己创建好的host句柄,可以直接使用add方法添加,而反向查没有,所以要使用django为我们提供的set句柄。
#正向增加
admin_obj = models.HostAdmin.objects.get(username='xs') host_list = models.Host.objects.filter(id__lt=3) admin_obj.host.add(*host_list)
#反向添加
host_obj = model.Host.objects.get(id=3) admin_list = models.HostAdmin.objects.filter(id__gt=1) host_obj.hostadmin_set.add(*admin_list)
查:
#正向查 admin_obj = models.HostAdmin.objects.get(username='xs') print admin_obj.host.all() #反向查 host_obj = models.Host.objects.get(id=3) print host_obj.hostadmin_set.all()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class Host1(models.Model): hostname = models.CharField(max_length=32) port = models.IntegerField() class HostAdmin1(models.Model): username = models.CharField(max_length=32) email = models.CharField(max_length=32) host = models.ManyToManyField(Host1,through='HostRelation') class HostRelation(models.Model): host =models.ForeignKey(Host1) admin =models.ForeignKey(HostAdmin1)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#增 #models.HostRelation.objects.create(host_id=1,admin_id=1) #查 relationList = models.HostRelation.objects.all() for item in relationList: print item.host.hostname print item.admin.username