🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC=1,5] >[success] # 表的三种对应方式 >[info] ## 一对一 * * * * * <br> >[info] ## 一对多 -- models.ForeignKey ~~~ class Book(models.Model): name = models.CharField(max_length=32) price = models.IntegerField() pub_data = models.DateField(auto_now_add=True) publish = models.ForeignKey('Publish') class Publish(models.Model): name = models.CharField(max_length=32) city = models.CharField(max_length=32) ~~~ >[danger] ##### 增加 * 第一种方法通过真实的字段,也就是在_id添加 ~~~ def index(request): from cm import models obj = models.Book.objects.create( name = "c#", price = 200, pud_data = '2008-08-08', publish_id= 1 # 这里保存的 关联表名_id ) print(obj) return HttpResponse('ok') ~~~ * 第二种通过对象添加,具体的操作是先查找出关联对象,把关联对象直接赋值給属性 >[danger] ##### 正向查 * 正向查找通过对象包含查找 ~~~ def index(request): from cm import models obj = models.Book.objects.filter(name="python") # 因为 books 和publish 关联,当book.publish ,是publish 库的对象 print(obj[0].publish.city) return HttpResponse('ok') ~~~ >[danger] ##### 反向查找--反向指的是没有ForeignKey * 通过查询反向数据库的对象,给到正向数据库 ~~~ def index(request): from cm import models obj = models.Publish.objects.filter(name="理工")[0] obj1 = models.Book.objects.filter(publish=obj).values("name") print(obj1) return HttpResponse('ok') ~~~ * 第二种通过set反查,也是获取反向数据库的对象,利用正向的名字_set ~~~ def index(request): from cm import models obj = models.Publish.objects.filter(name="理工")[0] obj1 = obj.book_set.all() print(obj1) return HttpResponse('ok') ~~~ * 第三种直接属性双下滑下接字段,例如publish__name ~~~ def index(request): from cm import models obj = models.Book.objects.filter(publish__name="理工").values("name") print(obj) return HttpResponse('ok') ~~~ * * * * * <br> >[info] ## 多对多 -- ManyToManyFlied >[danger] ### 利用ForeignKey 创建多对多 ~~~ class HOST(models.Model): uid = models.AutoField(primary_key=True) host = models.CharField(max_length=32,db_index=True) ip = models.GenericIPAddressField(max_length=32,db_index=True) port = models.IntegerField() b = models.ForeignKey('Business',to_field = 'id') class Application(models.Model): name = models.CharField(max_length=32) class HostToApp(models.Model): hobj = models.ForeignKey('HOST',to_field='uid') aobj = models.ForeignKey('Application', to_field='id') ~~~ ![](https://box.kancloud.cn/cceb07364cdaa78f1f99c2ac28a30a8c_561x562.png) * * * * * <br> >[danger] ### 利用传统多对多 * 一个电影有多个导演,多个导演合作导了一部电影 ~~~ class movies(models.Model): name = models.CharField(max_length=20) price = models.IntegerField() authors = models.ManyToManyField('Author') class Author(models.Model): name = models.CharField(max_length=32) ~~~ 创建出来第三个关联表 ![](https://box.kancloud.cn/599ae96b300f0048039ee0741ec351e2_1099x403.png) * * * * * <br> >[danger] ##### 查 通过中间桥的字段可以直接插第二个表 ~~~ obj_movies = models.movies.objects.filter(id=2) obj_author = models.Author.objects.all() print(obj_movies[0].authors.values('name')) ~~~ >[danger] ##### 增加 -- add * 给一组对象 ~~~ obj_movies = models.movies.objects.create(name="大话西游", price="10") obj_author = models.Author.objects.all() obj_movies.authors.add(*obj_author) ~~~ * * * * * * 给 id ~~~ obj = [1,2] obj1 = models.movies.objects.filter(name="python") obj1[0].authors.add(*obj) ~~~ * * * * * >[danger] ##### 删除remove 删除和增加原理一样 ~~~ obj1 = models.Book.objects.filter(name="python") obj1[0].authors.remove(*obj) return HttpResponse('ok') ~~~