django【orm操作】
生活随笔
收集整理的這篇文章主要介紹了
django【orm操作】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、ORM表結構
1 class Publisher(models.Model): 2 name = models.CharField(max_length=30, verbose_name="名稱") 3 address = models.CharField("地址", max_length=50) 4 city = models.CharField('城市',max_length=60) 5 state_province = models.CharField(max_length=30) 6 country = models.CharField(max_length=50) 7 website = models.URLField() 8 9 class Meta: 10 verbose_name = '出版商' 11 verbose_name_plural = verbose_name 12 13 def __str__(self): 14 return self.name 15 16 class Author(models.Model): 17 name = models.CharField(max_length=30) 18 def __str__(self): 19 return self.name 20 21 class AuthorDetail(models.Model): 22 sex = models.BooleanField(max_length=1, choices=((0, '男'),(1, '女'),)) 23 email = models.EmailField() 24 address = models.CharField(max_length=50) 25 birthday = models.DateField() 26 author = models.OneToOneField(Author) 27 28 class Book(models.Model): 29 title = models.CharField(max_length=100) 30 authors = models.ManyToManyField(Author) 31 publisher = models.ForeignKey(Publisher) 32 publication_date = models.DateField() 33 price=models.DecimalField(max_digits=5,decimal_places=2,default=10) 34 def __str__(self): 35 return self.title?
二、ORM增加數據
from app01.models import *一、單表增加數據#create方式一: Author.objects.create(name='Alvin')#create方式二: Author.objects.create(**{"name":"alex"})#save方式一: author=Author(name="alvin") author.save()#save方式二: author=Author()author.name="alvin"author.save()二、1對多增加數據 #方式一:#Book.objects.create(title='vb',publisher_id=1, publication_date="2016-7-7",price=198)#方式二:#publisher_obj = Publisher.objects.get(id=3)#Book.objects.create(title='vb',publisher=publisher_obj, publication_date="2016-7-7",price=198)備注:將 publisher_id=2 改為 publisher=publisher_obj三、多對多增加數據1.#為id=1這本書增加2個作者。將數據增加到第三張表中# 正向增加author1=Author.objects.get(id=1)author2=Author.objects.filter(name='alvin')[0]book=Book.objects.filter(id=1)[0]book.authors.add(author1,author2)2.#反向增加#將多本書增加到一個作者book=models.Book.objects.filter(id__gt=1)authors=models.Author.objects.filter(id=1)[0]authors.book_set.add(*book)authors.book_set.remove(*book)3.如果第三張表是通過models.ManyToManyField()自動創建的,那么綁定關系只有上面一種方式#如果第三張表是自己創建的:class Book2Author(models.Model):author=models.ForeignKey("Author")Book= models.ForeignKey("Book")#那么就還有一種方式:author_obj=models.Author.objects.filter(id=2)[0]book_obj =models.Book.objects.filter(id=3)[0]s=models.Book2Author.objects.create(author_id=1,Book_id=2)s.save()s=models.Book2Author(author=author_obj,Book_id=1)s.save()?
三、ORM刪除數據
1.刪除BOOK表id=1的數據Book.objects.filter(id=1).delete()2.刪除一個出版社. 注意與出版相關聯的書也會被刪除?
Publisher.objects.filter(id=3).delete()3.刪除id=6這本書和作者id=1的關聯(刪除第三張關系表數據)book = Book.objects.filter(id=6)[0] # 找到這本書的對象author = Author.objects.filter(id=1)[0] # 找到這個作者的對象 author.book_set.remove(book) # 反向刪除book.authors.remove(author) # 正向刪除
?
四、ORM更新數據
1.對象更新(所有字段更新,性能低)ret = Publisher.objects.get(id=2)ret.name = "復旦大學"ret.save()2.級聯方法更新Publisher.objects.filter(id=1).update(city="北京市")?
五、ORM查詢數據
1.了不起的雙下劃線(__)之單表條件查詢 # models.Tb1.objects.filter(id__lt=10, id__gt=1) # 獲取id大于1 且 小于10的值 # # models.Tb1.objects.filter(id__in=[11, 22, 33]) # 獲取id等于11、22、33的數據 # models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in # # models.Tb1.objects.filter(name__contains="ven") # models.Tb1.objects.filter(name__icontains="ven") # icontains大小寫不敏感 # # models.Tb1.objects.filter(id__range=[1, 2]) # 范圍bettwen and # # startswith,istartswith, endswith, iendswith,2.了不起的雙下劃線(__)之多表條件關聯查詢 # 正向查找(條件)# ret3=models.Book.objects.filter(title='Python').values('id') # print(ret3)#[{'id': 1}] #正向查找(條件)之一對多 ret4=models.Book.objects.filter(title='Python').values('publisher__city')print(ret4) #[{'publisher__city': '北京'}]#正向查找(條件)之多對多ret5=models.Book.objects.filter(title='Python').values('author__name')print(ret5)ret6=models.Book.objects.filter(author__name="alex").values('title')print(ret6)#注意#正向查找的publisher__city或者author__name中的publisher,author是book表中綁定的字段#一對多和多對多在這里用法沒區別# 反向查找(條件)#反向查找之一對多:ret8=models.Publisher.objects.filter(book__title='Python').values('name')print(ret8)#[{'name': '人大出版社'}] 注意,book__title中的book就是Publisher的關聯表名 ret9=models.Publisher.objects.filter(book__title='Python').values('book__authors')print(ret9)#[{'book__authors': 1}, {'book__authors': 2}]#反向查找之多對多:ret10=models.Author.objects.filter(book__title='Python').values('name')print(ret10)#[{'name': 'alex'}, {'name': 'alvin'}]#注意#正向查找的book__title中的book是表名Book#一對多和多對多在這里用法沒區別?
轉載于:https://www.cnblogs.com/weibiao/p/6871803.html
總結
以上是生活随笔為你收集整理的django【orm操作】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高仿微信对话列表滑动删除效果
- 下一篇: 2015年07月04日