千呼萬喚始出來~~~噹噹噹,終於系統要寫django的ORM操做啦!!!這裏記錄的是django操做數據庫表一對多、多對多的表建立及操做。對於操做,咱們只記錄連表相關的內容,介紹增長數據和查找數據,由於能查到就能夠用常規方法進行修改,怎麼加進來就怎麼刪掉,都是大同小異,就再也不贅述了~~~python
一對多sql
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添加 django
models.UserInfo.objects.create(username='Eva_J',age=18,user_type_id=1)
2.直接添加外鍵的對象app
obj = models.UserType(caption='test') obj.save() models.UserInfo.objects.create(username='Eva_J',age=18,user_type=obj)
查:ide
正向查詢:根據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查userinfospa
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')
多對多code
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):
未完待續......