程序媛

python——Django(ORM连表操作)

  千呼万唤始出来~~~当当当,终于系统要写django的ORM操作啦!!!这里记录的是django操作数据库表一对多、多对多的表创建及操作。对于操作,我们只记录连表相关的内容,介绍增加数据和查找数据,因为能查到就可以用常规方法进行修改,怎么加进来就怎么删掉,都是大同小异,就不再赘述了~~~

一对多

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='Eva_J',age=18,user_type_id=1)

 

2.直接添加外键的对象

obj = models.UserType(caption='test')
obj.save()
models.UserInfo.objects.create(username='Eva_J',age=18,user_type=obj)

查:

  正向查询:根据userinfo查usertype

result = models.UserInfo.objects.filter(user_type__caption='CEO')
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='eva') #用字段条件查询
    print '-->4',models.UserInfo.objects.filter(user_type=result) #用对象条件查询

    user_type_obj = models.UserType.objects.get(userinfo__username='eva')
    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='alex')
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='alex')
    print admin_obj.host.all()
 #反向查
    host_obj = models.Host.objects.get(id=3)
    print host_obj.hostadmin_set.all()

 

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)
自定义多对多表创建
    #
    #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
自定义多对多表操作

 

其他(selecte_related && query):

   select_related是用来为连表查询做优化的,我们在查询外键关联的表的时候,都应该使用select_related,这样,只需要执行一条命令就可以把相关的字段都查到了。下面就来看看select_related的作用吧~

 例:    

 1 class UserType(models.Model):
 2           caption = models.CharField(max_length=32)
 3           def __unicode__(self):
 4               return self.caption
 5 
 6 
 7       class UserInfo(models.Model):
 8           user_type = models.ForeignKey(UserType)# user_type对象中封装id,caption
 9           username = models.CharField(max_length=32)
10           age = models.IntegerField()
11           def __unicode__(self):
12              return self.username
表结构

 

python代码: 

result = models.UserInfo.objects.all()
      result_sr = models.UserType.objects.select_related().all()
      print result.query
      print result_sr.query

生成的sql:

      SELECT "app01_userinfo"."id", "app01_userinfo"."user_type_id", "app01_userinfo"."username", "app01_userinfo"."age" FROM "app01_userinfo"
      SELECT "app01_usertype"."id", "app01_usertype"."caption" FROM "app01_usertype"

  这里顺便再介绍一下query方法,可以获取到django为我们生成的sql内容~就是这么炫酷!

其他(F和Q):

 未完待续......

posted @ 2016-04-14 15:26  Eva_J  阅读(7582)  评论(0编辑  收藏  举报