gpt4 book ai didi

python - Django REST Framework -- 序列化多对多表关系

转载 作者:太空狗 更新时间:2023-10-30 00:09:07 25 4
gpt4 key购买 nike

我正在为一个网站创建一个 API,我有一个带有 Books 的表,另一个带有 Hashtags(基本上是一个 hashtags 的目录,例如“#traveling”)和一个用于建立多对多的中间表书籍和标签之间的关系。我的模型是:

# models.py
class Books(models.Model):
id_books = models.AutoField(primary_key=True)
title = models.CharField(max_length=150)

class Hashtags(models.Model):
id_hashtags = models.AutoField(primary_key=True)
hashtag = models.CharField(max_length=150)

class Books_Hashtags(models.Model):
id_books_hashtags = models.AutoField(primary_key=True)
id_books = models.ForeignKey(Books, on_delete=models.CASCADE)
id_hashtags = models.ForeignKey(Hashtags, on_delete=models.CASCADE)
class Meta:
unique_together = ("id_books", "id_hashtags")

我想要一个提供如下输出的 API:

[
{
"id_books": 1,
"title": "The Hobbit",
"hashtags": [
{
"id_hashtags": 1,
"hashtag": "fantasy"
},
{
"id_hashtags": 2,
"hashtag": "fiction"
},
{
"id_hashtags": 3,
"hashtag": "middle earth"
},
]
}
]

为此,我根据我在网上找到的一些示例,使用 DRF 对上述模型进行了序列化:

#serializers.py
from rest_framework import serializers
from app.models import Books, Books_Hashtags, Hashtags

class HashtagsSerializer(serializers.ModelSerializer):
hashtags = serializers.CharField()
class Meta:
model = Hashtags
fields = ('hashtag',)

class Books_HashtagsSerializer(serializers.ModelSerializer):
id_hashtag = HashtagsSerializer(many=True, read_only=True)
class Meta:
model = Books_Hashtags
fields = ('id_hashtags',)

class BooksSerializer(serializers.ModelSerializer):
id_books = serializers.IntegerField()
id_books_hashtags = Books_HashtagsSerializer(many=True, read_only=True)
title = serializers.CharField()

class Meta:
model = Books
fields = ('id_books', 'title', 'id_books_hashtags',)

我的观点是这样的:

#views.py
from ntgBackend.models import Books
from rest_framework import viewsets
from api.serializers import BooksSerializer

class BooksViewSet(viewsets.ModelViewSet):
serializer_class = BooksSerializer
queryset = Books.objects.all()

到目前为止,我已经设法从 API 获得以下输出:

[
{
"id_books": 1,
"title": "The Hobbit",
}
]

但是没有关于主题标签的信息,我知道它们存在于数据库中。我将 DRF 3.8.2 与 MySQL 数据库和 Django 2.0.2 一起使用。请帮忙!

最佳答案

我遇到了同样的问题,并通过一些研究得出了以下结论:

在 models.py 中,仅修改您的 Book 模型以包含一个带有 Hashtags 的 ManyToManyField,使用 Books_Hashtagas 作为您的中间表。还要确保在第一本书之前声明您的标签模型以供引用

# models.py
class Books(models.Model):
id_books = models.AutoField(primary_key=True)
title = models.CharField(max_length=150)
hashtags = models.ManyToManyField(Hashtags, related_name='hashTg', through="Books_Hashtags"

在你的序列化文件中一定要添加 depth = 1 以获得你需要的东西,否则你只会得到标签 id(这仅适用于 ListAPI)

#serializers.py
class BooksSerializer(serializers.ModelSerializer):
class Meta:
model = Books
#In case you want to filter out some fields:
#fields = ('field_A','field_B' )
fields = '__all__'
depth = 1

在 View 中我使用泛型,因为它们解决了我的大部分需求:

#views.py
from ntgBackend.models import Books
from rest_framework import generics
from api.serializers import BooksSerializer

class BooksViewSet(generics.ListCreateAPIView):
serializer_class = BooksSerializer
queryset = Books.objects.all()

刚接触这个,希望对你有帮助。除了 Django(2.0.4),我使用的版本与您几乎相同。

祝你好运

关于python - Django REST Framework -- 序列化多对多表关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50468672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com