gpt4 book ai didi

Python Django REST 调用返回对象编号而不是对象名称

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:18 25 4
gpt4 key购买 nike

我是 Python 新手,我想我的序列化不正确。

这是 REST 调用结果: enter image description here

authors/models.py:

    from django.db import models
from django.contrib.auth.models import User


# Create your models here.

class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name


class Book(models.Model):
auto_increment_id = models.AutoField(primary_key=True)
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, blank=False, null=False, related_name='book_author')
contents = models.TextField('Contents', blank=False, null=False)
def __str__(self):
return self.name

authors/views.py

from authors.models import Book, Author
from authors.serializers import BookSerializer, AuthorSerializer
from django.shortcuts import render
from rest_framework import generics


class ListCreateBooks(generics.ListCreateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer

class ListCreateAuthor(generics.ListCreateAPIView):
queryset = Author.objects.all()
serializer_class = AuthorSerializer

authors/serializers.py

from authors.models import Book, Author
from rest_framework import serializers

class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('name', 'author')

class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
book_author = 'name'

我是 Django 新手,但我尝试了很多东西,在我的views.py 中,我添加了另一个名为 AuthorSerializer 的类,该类从我在序列化器中创建的相应类导入,但后来我意识到我不知道如何将我的 ListCreateAuthor 添加到:

 url(r'^api-auth/', ListCreateBooks.as_view(), name='list_books')

我用 ListCreateAuthor.as_view() 添加了另一个参数,这给了我一个立即错误(而且也没有多大意义)我在这里走错路了吗?我该怎么办?解决这个问题吗?

EDIT: @Abdulafaja gave a partial answer, which did solve the read, but now after checking a POST insert - it gives an error for create or update.

Django rest_framework's documentation (由@Abdulafaja提供的链接)它只给出了嵌套序列化器的一个示例,并且它是一对多关系(专辑->轨道),但我的是一对一关系(书籍->作者),所以我不知道如何序列化嵌套特征。该API需要为前端提供所有CRUD功能。

谢谢。

EDITED serializers.py by Abdullah:

from authors.models import Book, Author
from rest_framework import serializers


class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
book_author = 'name'


class BookSerializer(serializers.ModelSerializer):
author = AuthorSerializer(many=False)

class Meta:
model = Book
fields = ('name', 'author')

def create(self, validated_data):
author, _ = Author.objects.get_or_create(name=validated_data.get('author').get('name'))
return Book.objects.create(name=validated_data.get('name'), author=author)

def update(self, instance, validated_data):
author = validated_data.get('author')
if author:
instance.author = Author.objects.get_or_create(name=author.get('name'))
instance.name = validated_data.get('name', instance.name)
instance.save()
return instance

最佳答案

据我了解,您希望在 api 请求中获取作者的详细信息,而不仅仅是他们的编号?

做到这一点的方法是在模型序列化器中设置深度。


class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('name', 'author')
depth=1

深度选项应设置为一个整数值,该值指示在恢复为平面表示之前应遍历的关系的深度。即 1、2、3...

来源:http://www.django-rest-framework.org/api-guide/serializers/

关于Python Django REST 调用返回对象编号而不是对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39287577/

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