码迷,mamicode.com
首页 > 其他好文 > 详细

Django模型多对多关系

时间:2020-10-05 21:50:52      阅读:21      评论:0      收藏:0      [点我收藏+]

标签:obj   产生   django   any   save   har   label   update   first   

多对多关系 ManyToManyField

以下是一些小说和小说标签,以及小说和标签之间的多对多关系

宫锁心玉  =>  穿越、古装、言情

美女总裁  =>  都市、言情

斗破苍穹  =>  穿越、玄幻、言情

都市重生  =>  都市、玄幻

创建模型类Fictions、Labels,小说类和小说标签类

模型类

from django.db import models

# 小说类
class Fictions(models.Model):
    fname = models.CharField(max_length=30)

    class Meta():
        db_table = fictions

# 标签类
class Labels(models.Model):
    lname = models.CharField(max_length=30)
    # 在标签表中关联小说表,小说表的一条数据关联标签表的多条数据,标签类的一条数据关联小说表的多条数据
    # django会自动生成第三个表(该表描述了小说和标签的关系)
    fid = models.ManyToManyField(to=Fictions)

    class Meta():
        db_table = labels

根路由

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path(admin/, admin.site.urls),
    path(app/, include(myapp.urls)),
]

子路由

from django.urls import path, re_path

from . import views

urlpatterns = [
    path(mtm_add/, views.manytomany_add),
    path(mtm_search/, views.manytomany_search),
    path(mtm_update/, views.manytomany_update),
    path(mtm_delete/, views.manytomany_delete),
]

视图

from django.http import HttpResponse
from . import models

# =========================模型多对多关系======================================
def manytomany_add(request):
    # 添加小说
    fiction_01 = models.Fictions(fname=宫锁心玉/)
    fiction_02 = models.Fictions(fname=美女总裁/)
    fiction_03 = models.Fictions(fname=斗破苍穹/)
    fiction_04 = models.Fictions(fname=都市重生/)
    fiction_01.save()
    fiction_02.save()
    fiction_03.save()
    fiction_04.save()
    # 添加标签
    label_01 = models.Labels(lname=穿越/)
    label_02 = models.Labels(lname=古装/)
    label_03 = models.Labels(lname=言情/)
    label_04 = models.Labels(lname=都市/)
    label_05 = models.Labels(lname=玄幻/)
    label_01.save()
    label_02.save()
    label_03.save()
    label_04.save()
    label_05.save()
    # 将小说和标签关联起来
    fictions = models.Fictions.objects.filter(id__gte=0)
    label = models.Labels.objects.filter(id__gte=0)
    label[0].fid.add(fictions[0], fictions[2])
    label[1].fid.add(fictions[0])
    label[2].fid.add(fictions[0], fictions[1], fictions[2])
    label[3].fid.add(fictions[1], fictions[3])
    label[4].fid.add(fictions[2], fictions[3])

    return HttpResponse(<script>alert("success");</script>)

def manytomany_search(request):
    # 通过小说查标签
    fiction = models.Fictions.objects.get(id=1)
    print(fiction.fname)
    print(fiction.labels_set.all().values())

    # 通过标签查小说
    label = models.Labels.objects.get(id=2)
    print(label.lname)
    print(label.fid.all().values())

    return HttpResponse(<script>alert("success");</script>)

def manytomany_update(request):
    # 修改小说名称并通过小说对象修改标签
    fiction = models.Fictions.objects.get(id=1)
    fiction.fname = 宫锁心玉
    fiction.save()
    update_label = fiction.labels_set.all().first()
    update_label.lname = 穿越
    update_label.save()

    # 修改标签名称并通过标签对象修改标签
    label = models.Labels.objects.get(id=3)
    label.lname = 言情
    label.save()
    update_fiction = label.fid.all().last()
    update_fiction.fname = 斗破苍穹
    update_fiction.save()

    return HttpResponse(<script>alert("success");</script>)

def manytomany_delete(request):
    # 删除小说表的数据,标签表不会产生影响,但会删除掉关系表中与该数据有关的记录
    # delete_fiction = models.Fictions.objects.get(id=4)
    # delete_fiction.delete()

    # 删除标签表的数据,小说表不会产生影响,但会删除掉关系表中与该数据有关的记录
    delete_label = models.Labels.objects.get(id=5)
    delete_label.delete()

    return HttpResponse(<script>alert("success");</script>)

我们无需自己创建关系表,Django会自动生成第三个关系表(该表描述了小说和标签的关系)

 

Django模型多对多关系

标签:obj   产生   django   any   save   har   label   update   first   

原文地址:https://www.cnblogs.com/glz666/p/13762649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!