gpt4 book ai didi

python - DRF AttributeError 类型对象 'QuerySet' 没有属性 'nom'

转载 作者:行者123 更新时间:2023-12-01 04:47:28 26 4
gpt4 key购买 nike

我正在制作我的第一个 DRF api,我收到一个我不明白的奇怪的 AttributeError 。当我尝试使用 GET 请求访问我的数据时,出现以下错误:

Got AttributeError when attempting to get a value for field nom on serializer ExpediteurSerializer. The serializer field might be named incorrectly and not match any attribute or key on the type instance. Original exception text was: type object 'QuerySet' has no attribute 'nom'.

我想我在我的serializers.py 文件中做了一些意想不到的事情。以下是我的代码的一些片段。

models.py

from django.db import models

# Create your models here.

class Expediteur(models.Model):
nom = models.CharField(max_length=50)
prenom = models.CharField(max_length=100)
adresse = models.CharField(max_length=200)
tel = models.IntegerField()

views.py

from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from polls.serializers import ExpediteurSerializer, DestinataireSerializer, LettrePrioSerializer, TypeLettreRecoSerializer, LettreRecoSerializer, TimbrePrioSerializer, TimbreRecoSerializer
from polls.models import Expediteur, Destinataire, LettrePrio, TypeLettreReco, LettreReco, TimbrePrio, TimbreReco
from rest_framework import status, HTTP_HEADER_ENCODING
from django.shortcuts import render, get_object_or_404

import json
import datetime

from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.shortcuts import render, get_object_or_404
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth import login
from django.core.exceptions import ObjectDoesNotExist
from django.views.decorators.csrf import csrf_exempt
from django.utils.timezone import get_current_timezone

# Create your views here.

class ExpeViewSet(APIView):
serializer_class = ExpediteurSerializer

def get(self, request):
queryset = Expediteur.objects.all()
serializer = ExpediteurSerializer(queryset)
return Response(serializer.data)

def post(self, request):
serializer = self.serializer_class(data=request.DATA)
return Response(status=status.HTTP_201_CREATED)

序列化器.py

from rest_framework import serializers
from polls.models import Expediteur, Destinataire, LettrePrio, TypeLettreReco, LettreReco, TimbrePrio, TimbreReco


class ExpediteurSerializer(serializers.Serializer):
nom = serializers.CharField(required=True, allow_blank=False, max_length=50)
prenom = serializers.CharField(required=True, allow_blank=False, max_length=100)
adresse = serializers.CharField(required=True, allow_blank=False, max_length=200)
tel = serializers.IntegerField(required=True)

def create(self, validated_data):
return Expediteur.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.nom = validated_data.get('nom', instance.nom)
instance.prenom = validated_data.get('prenom', instance.prenom)
instance.adresse = validated_data.get('adresse', insatnce.adresse)
instance.tel = validated_data.get('tel', instance.tel)
instance.save()
return instance

最佳答案

我认为您在 View 中缺少许多标志,如下所示:

ExpediteurSerializer(queryset, many=True)

一般来说,通过充分利用 DRF,您可以大大简化代码。首先,根据 documentation 将 ExpediteurSerializer 设为 ModelSerializer 。

然后您还可以摆脱创建和更新方法,DRF 会为您处理所有这些事情。我强烈建议您查看 ModelViewSets,它们让您的生活变得非常轻松。

关于python - DRF AttributeError 类型对象 'QuerySet' 没有属性 'nom',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29123114/

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