当前位置:网站首页>Note: restframe work records many to one tables, how to serialize in that table (reverse query)
Note: restframe work records many to one tables, how to serialize in that table (reverse query)
2022-07-29 03:59:00 【liyu5543】
problem : There are three tables , One of them is the main table , The other two are subsidiary tables , The subsidiary table has foreign keys pointing to the main table . At serialization , You need to serialize the main table , But you need to get data from two subsidiary tables .
The biggest highlight here is : Need to be in the attached table models in , Set that related_name=‘xxx’ Then use this ‘xxx’ Be the field in the main table sequence . Also, it must be set many=True
Specifically, the following code :
model.py
The picture table and the download address table are subsidiary tables , Point to the main table . Don't look at the classification type table here
Note that both the picture table and the download address table must be set related_name Parameters
from django.db import models
# Classification type surface
class Category(models.Model):
title = models.CharField(max_length=32)
# picture URL surface
class ImgURL(models.Model):
imgurl = models.URLField()
movie = models.ForeignKey(to='Movie', on_delete=models.CASCADE, related_name='img_movie')
# Download address surface
class DownloadUrl(models.Model):
downloadurl = models.CharField(max_length=300)
movie = models.ForeignKey(to='Movie', on_delete=models.CASCADE, related_name='download_movie')
# The movie surface
class Movie(models.Model):
title = models.CharField(max_length=300)
rate = models.FloatField()
pub_time = models.DateTimeField(auto_now_add=True)
content = models.TextField()
category = models.ManyToManyField(to=Category)
–> The general idea is to serialize in the movie table , Bring pictures and download addresses directly , But it is impossible to follow the ordinary method ,
2. Writing of serialization :
class DownloadUrlSerializer(serializers.ModelSerializer):
class Meta:
model = DownloadUrl
fields = '__all__'
class ImgUrlSerializer(serializers.ModelSerializer):
class Meta:
model = ImgURL
fields = ['imgurl']
class MovieSerializer(serializers.ModelSerializer):
img_movie = ImgUrlSerializer(many=True) # Be sure to pay attention to this parameter img_movie It's from the picture table above related_name, And you have to set many=True
download_movie = DownloadUrlSerializer(many=True) # Be sure to pay attention to this parameter download_movie It's from the download address table above related_name, And you have to set many=True
class Meta:
model = Movie
fields = ['id', 'title', 'rate', 'pub_time', 'content', 'category', 'img_movie', 'download_movie'] # Add the above two parameters here
depth = 3
The focus is on the following code :
# Be sure to pay attention to this parameter img_movie It's from the picture table above related_name, And you have to set many=True
img_movie = ImgUrlSerializer(many=True)
# Be sure to pay attention to this parameter download_movie It's from the download address table above related_name, And you have to set many=True
download_movie = DownloadUrlSerializer(many=True)
Then the above two sentences of code are introduced into the serialization of the main table movie table , And then in views You can directly serialize movies in
as follows :
3. views.py
class MovieView(APIView):
def get(self, request, *args, **kwargs):
movie = Movie.objects.all()
ser = MovieSerializer(instance=movie, many=True)
return Response(ser.data)
The output of the page is as follows :

边栏推荐
- Flutter 启动白屏
- Microcomputer principle and interface technology
- Solve the problem of garbled code when opening the project code in idea
- Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率
- I. creation and constraint of MySQL table
- The const keyword of ES6 declares variables
- Getting started with caspin
- Data too long for column 'xxx' at row 1 solution
- The function parameters of the new features of ES6 are assigned initial values and rest parameters
- 小马智行进军前装量产,从自研域控制器入手?
猜你喜欢

3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘

Process tracking of ribbon principle

HCIP BGP

What have I learned from 200 machine learning tools?

SQL window function

LDP -- label distribution protocol

Typescript from getting started to mastering (XX) function generics
![[principle] several ways of horizontal penetration](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)
[principle] several ways of horizontal penetration

大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒

Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
随机推荐
BGP的基础配置---建立对等体、路由宣告
1985-2020 (8 Editions) global surface coverage download and introduction
sql
RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密
How fast does it take to implement a super simple programming language?
Android view system and custom view Series 1: (kotlin version)
小马智行进军前装量产,从自研域控制器入手?
Lucifer 98 life record ing
First knowledge of C language (3)
Uni app internationalization
[introduction to C language] zzulioj 1031-1035
@Configuration (proxybeanmethods = false) what's the use of setting this to false
Configmap配置与Secret加密
第一个ALV程序2
路西法98-生活记录ing
nacos注册中心
Solve the delay in opening the console of Google browser
The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
Let variable declaration feature of ES6 new feature and its case
Tristate gate