Django prefetch_related()方法
创始人
2024-12-26 22:37:48
0

prefetch_related的作用

prefetch_related()是 Django ORM 中用于优化查询性能的另一个重要方法,尤其在处理多对多(ManyToMany)关系和反向关系时非常有用。它允许你预加载相关对象,从而减少数据库查询次数。

1,创建应用

Test/app13

 python manage.py startapp app13

2,注册应用

Test/Test/settings.py

3,添加应用路由

from django.contrib import admin from django.urls import path, include  urlpatterns = [      path('app13/', include('app13.urls')), ] 

4,添加模型

Test/app13/models.py

from django.db import models  class Author(models.Model):     name = models.CharField(max_length=100)  class Book(models.Model):     title = models.CharField(max_length=100)     authors = models.ManyToManyField(Author)  class Review(models.Model):     book = models.ForeignKey(Book, on_delete=models.CASCADE)     rating = models.IntegerField()

 

5,添加视图函数

Test/app13/views.py

from django.shortcuts import render from .models import Book, Author, Review  def book_list(request):     # 使用 prefetch_related 预加载作者和评论信息     books = Book.objects.prefetch_related('authors', 'review_set').all()      # 准备传递给模板的上下文     context = {         'books': books,     }      # 渲染并返回响应     return render(request, '13/book_list.html', context)

 

 6,添加html代码

Test/templates/13/book_list.html

            Title    {% for book in books %}     

{{ book.title }}

Authors:

    {% for author in book.authors.all %}
  • {{ author.name }}
  • {% endfor %}

Reviews:

    {% for review in book.review_set.all %}
  • Rating: {{ review.rating }}
  • {% endfor %}
{% endfor %}

7,添加路由地址

 Test/app13/urls.py

from django.urls import path from . import views  urlpatterns = [      path('book_list/', views.book_list, name='book_list'), ]

 

 8,添加数据

Test/populate_db.py

import random   import os import django  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Test.settings') django.setup() from app13.models import Author, Book, Review    # 创建随机作者 for _ in range(10):     author = Author(name=f'Author {_}')     author.save()  # 创建随机书籍 for _ in range(20):     book = Book(title=f'Book Title {_}')     book.save()     # 随机选择1-3个作者     authors = Author.objects.all()     book.authors.set(random.sample(list(authors), random.randint(1, 3)))  # 创建随机评论 for _ in range(50):     review = Review(         book=random.choice(Book.objects.all()),         rating=random.randint(1, 5)     )     review.save()  

 

 9,访问页面

http://127.0.0.1:8000/app13/book_list/

 

相关内容

热门资讯

WPK透视辅助!德州局脚本,w... WPK透视辅助!德州局脚本,wpk德州局透视(切实是有挂)1、超多福利:超高返利,海量正版游戏,wp...
微扑克透视辅助!pokemmo... 微扑克透视辅助!pokemmo辅助器手机版下载,hhpoker开挂教程(一贯是真的有挂)1、微扑克透...
WPK透视辅助!扑克之星辅助,... WPK透视辅助!扑克之星辅助,wepoker辅助是真的假的(真是真的有挂)wepoker辅助是真的假...
wpk透视辅助!hhpoker... wpk透视辅助!hhpoker是内部控制吗,wepoker破解工具(好像真的有挂)1、hhpoker...
微扑克透视辅助!hhpoker... 微扑克透视辅助!hhpoker德州透视,xpoker透视辅助(好像真的是有挂)1)hhpoker德州...
微扑克透视辅助!黑侠破解wep... 微扑克透视辅助!黑侠破解wepoker,wepoker辅助分析器(切实是有挂)1、首先打开wepok...
wpk透视辅助!aapoker... wpk透视辅助!aapoker怎么设置抽水,大菠萝手游辅助(总是真的有挂)运aapoker怎么设置抽...
wpk透视辅助!wepoker... wpk透视辅助!wepoker软件辅助程序,wepoker辅助器安装包定制(其实是有挂)小薇(辅助器...
微扑克透视辅助!aapoker... 微扑克透视辅助!aapoker怎么提高中牌率,hhpoker软件安装包(确实真的有挂)1、进入游戏-...
WPK透视辅助!wepoker... 您好,wepoker透视器免费这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...