from django.db import models
# Create your models here.
class Demo(models.Model):
demo_sha1 = models.CharField(max_length=50)
demo_name = models.CharField(max_length=30)
demo_content = models.CharField(max_length=300)
# favor = models.ManyToManyField('Service')
def __str__(self):
return self.demo_name
class Service(models.Model):
service_sha1 = models.CharField(max_length=50)
favor = models.ManyToManyField('Demo')
def __str__(self):
return self.service_sha1
# insert into demo_demo (demo_sha1,demo_name,demo_content) values('004','s4','dada'),('005','s5','papa'),('006','s6','uaua');
# insert into demo_service(service_sha1) values('A'),('B'),('C'),('D');
# insert into demo_service_favor(service_id,demo_id) values('001','A'),('001','B'),('002','C'),('002','B'),('003','B'),('003','D'),('004','A'),('004','D');
# 多对多,查询(正查,反查)
a = Service.objects.get(pk=1)
print(a.favor.all())
b = Demo.objects.get(pk=1)
print(b.service_set.all())
django 多对多表格建立及正查反查
最新推荐文章于 2021-11-19 09:51:35 发布