使用版本python3.5+django1.11.4
创建django-admin startproject DjangoORM
创建App ./manage.py startapp ORM

from django.db import models
class Goods(models.Model):#商品
     g_name = models.CharField(max_length=20)
     g_price = models.DecimalField(max_digits=5, decimal_places=2)
     gc = models.ForeignKey("Category", null=True, on_delete=models.SET_NULL)#gc为外键,类别表为母表class Category(models.Model):#类别
     c_name = models.CharField(max_length=20) class Store(models.Model):#商家
     s_name = models.CharField(max_length=30)
     s_detail = models.TextField(blank=True, null=True)
     sc = models.ManyToManyField("Category")#与类别表进行多对多关联

多对多增删改查同时也适用一对多,一对一,修改少量参数即可

1.增
添加商家

Store.objects.create(s_name="商家A", s_detail="物美价廉,抄底折扣。。。。")
 Out[2]: <Store: Store object>Store(s_name="商家B", s_detail="大促销").save()

添加类别

Category.objects.create(c_name="电脑整机")
 <Category: Category object>Category(c_name="文具耗材").save()

增与改(增添子表或母表数据参照一对一的增,多对多重点在于关系表的对应关系变更)

创建商家C添加全部分类

Store.objects.create(s_name="商家C").sc.add(*(Category.objects.all()))#如果商户已存在则把create改成get
store = Store.objects.get(s_name="商家C")
 store.sc=(Category.objects.all())
 store.save()

创建商家D添加指定分类

store = Store.objects.create(s_name="商家D")
 category = Category.objects.filter(c_name__in=["电脑整机","文具耗材"])#单个改成get,全部改成all
 store.sc.add(*category)#add是追加模式
 store.sc.clear()#清空此商家的商品#让指定商品分类添加指定的商家,反向查询
 store = Store.objects.create(s_name="商家E")
 category = Category.objects.get(c_name="电脑整机")
 category.store_set.add(store)


效果与上面一样

让所有商家都添加这个分类

stores = Store.objects.all()
 category = Category.objects.get(c_name="电脑整机")
 category.store_set.add(*stores)category.store_set.clear()#让所有商家去除这个分类
 category.store_set.all().delete()#是删除store_set的所有商家
 #只有子表才有"子表名小写_set"的写法,得到的是一个QuerySet集合,后边可以接.add(),.remove(),.update(),.delete(),.clear()

 

2.查

(Store.objects.get(s_name="商家C")).sc.all()
 <QuerySet [<Category: Category object>, <Category: Category object>]>Category.objects.filter(store__s_name="商家C")
 <QuerySet [<Category: Category object>, <Category: Category object>]>#查找指定商家下面的所有分类
 Store.objects.get(s_name="商家C").sc.all()#写法:子表对象.子表多对多字段.过滤条件(all()/filter())
 <QuerySet [<Category: Category object>, <Category: Category object>]>

反向查询

Category.objects.filter(store__s_name="商家C")
 Out[25]: <QuerySet [<Category: Category object>, <Category: Category object>]>#母表对象.filter(子表表名小写__子表字段名="过滤条件")

从分类查询,查看那些商家里面发布指定的某一个分类

(Category.objects.get(c_name="电脑整机")).store_set.all()
 <QuerySet [<Store: Store object>, <Store: Store object>]>Store.objects.filter(sc=Category.objects.get(c_name="电脑整机"))
 <QuerySet [<Store: Store object>, <Store: Store object>]>Store.objects.filter(sc__c_name="电脑整机")#filter(子表外键字段__母表字段='过滤条件')
 <QuerySet [<Store: Store object>, <Store: Store object>]>Store.objects.filter(sc=category)##filter得到QuerySet,写法:filter(子表外键字段=母表主键对象),此处和一对多略有不同,是子表外键字段而不是外键字段_母表主键
 <QuerySet [<Store: Store object>, <Store: Store object>]>

   

3.删

让指定商家清空分类

s = Store.objects.get(s_name="商家C")
 c = Category.objects.all()
 s.sc = ""
 s.save()s=Store.objects.get(s_name="商家C")
 c = Category.objects.all()
 s.sc.remove(*c)s = Store.objects.get(s_name="商家C")
 s.sc.clear()

删除母表与子表关联关系
让所有商家去掉指定分类

s = Store.objects.all()
 c = Category.objects.get(c_name="电脑整机")
 c.store_set.remove(*s)c = Category.objects.get(c_name="电脑整机")
 c.store_set.clear()

#删除商家子表数据
删除所有指定分类的全部商家

c=Category.objects.get(c_name="电脑整机")
 c.store_set.all().delete()

删除所有商家

Store.objects.all().delete()
 ---------------------