一、一对一关系:
1、个人认为一对一关系的两个表,是可以合成一个表的,只不过合在一个表长度太长了,所以分成两个表,其实这两个表可以共用一个主键
2、一对一关系可以加在两个表中任意一个表中都行,创建方式
注意:以下所有代码都只作演示,没有对输入的参数进行判断,实际工作中是要进行严格的判断的,比如说输入字符是否为空,长度,是否符合需求等
models.OneToOneField()
例如,作者和作者详情表
class Author(models.Model):
author_name = models.CharField(max_length=10, null=False)
class AuthorDetail(models.Model):
author_age = models.IntegerField(null=False)
author_sex = models.IntegerField(choices=((1, "男"), (0, "女")),null=False)
author_address = models.CharField(max_length=100,null=False)
author = models.OneToOneField(to=Author,on_delete=models.CASCADE)
创建成功以后AuthorDetail表格中多了一列author_id
3、一对一关系,插入数据(注意,同样只做演示,真正开发过程中要对获到的参数做严格校验,比如说是否为空,长度等等)
@api_view(["POST", ])
def api_add_author(request):
new_author_sex = 0
author_name = request.POST.get("author_name")
author_age = request.POST.get("author_age")
author_sex = request.POST.get("author_sex")
author_address = request.POST.get("author_address")
author = Author.objects.create(author_name=author_name)
author_id = Author.objects.filter(id=author.id).first()
if author_id not in [None,""]:
if author_sex == "男":
new_author_sex = 1
AuthorDetail.objects.create(author=author, author_age=author_age, author_sex=new_author_sex,
author_address=author_address)
res ={"code":0,"msg":"插入成功"}
else:
res = {"code":1,"msg":"没有插入成功"}
return JsonResponse(res)
4、一对一关系,查询数据
查询名子叫小明,年纪是14的作者的id
我们要查到这条记录
这是我们看到了表格,也知道这条记录,那如果表格中数据多,你完全不知道数据库呢
@api_view(["POST", ])
def api_find_author(request):
author_name = request.POST.get("author_name")
author_age = request.POST.get("author_age")
print("author_age = %s"%author_age)
authors = Author.objects.filter(author_name=author_name)
print(authors)
ids = []
if len(authors) > 0:
for author in authors:
author_details = AuthorDetail.objects.filter(author=author, author_age=int(author_age))
if len(author_details)>0:
for author_detail in author_details:
ids.append(author_detail.author_id)
if len(ids)>0:
res = {"code": 0, "msg": "查询成功", "id": ids}
else:
res = {"code": 1, "msg": "该用户不存在"}
else:
res = {"code": 1, "msg": "作者名不存在"}
return JsonResponse(res)
二、一对多关系
就是外键关系,上篇文章说过了
https://www.jianshu.com/p/75fcecd9faed
三、多对多关系:
例如作者和书,一个书可以有好向个作者,一个作者也可以出好几本书
class Author(models.Model):
author_name = models.CharField(max_length=10, null=False)
class Book(models.Model):
book_name = models.CharField(max_length=30,null=False)
book_publisher = models.CharField(max_length=40,null=False)
author = models.ManyToManyField(Author) #多对多关系
数据库里多了一个表格
1、插入
def api_add_book(request):
book_name = request.POST.get("book_name")
book_publisher = request.POST.get("book_publisher")
book = Book.objects.create(book_name=book_name,book_publisher=book_publisher)
book.author.create(author_name="张三")
book.author.create(author_name="小红")
return JsonResponse({"code":0,"msg":"创建成功"})
2、查询
def api_select_book(request):
books = Book.objects.filter(author=11)
for book in books:
print(book.book_name)
return JsonResponse({"code":0,"msg":"查询成功"})
def api_select_book(request):
books = Book.objects.filter(author__author_name="张三")
for book in books:
print(book.book_name)
return JsonResponse({"code":0,"msg":"查询成功"})
关于 一对一 一对多 多对多关系的总结
为了减少数据表格的复杂性,优先使用一对一,一对多关系,最后选用多对多关系