django “TranslateCategory”对象在DRF中不可迭代

hvvq6cgz  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(111)

我使用Django Rest Framework并使用gunicorn运行我的代码。我得到了这个错误'TranslateCategory'对象不可迭代。
回溯:

Request Method: GET
Request URL: http://oralbekov.dias19.fvds.ru/api/translate/categories/1/

Django Version: 3.2.16
Python Version: 3.8.10
Installed Applications:
['jazzmin',
 'modeltranslation',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'rest_framework_simplejwt',
 'questions.apps.QuestionsConfig',
 'calendartodo.apps.CalendartodoConfig',
 'rites.apps.RitesConfig',
 'map.apps.MapConfig',
 'news.apps.NewsConfig',
 'translate.apps.TranslateConfig',
 'info.apps.InfoConfig',
 'reportederrors.apps.ReportederrorsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback (most recent call last):
  File "/var/www/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/var/www/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/var/www/hajjum/translate/views.py", line 30, in categories_id
    return Response(serializer.data ,status=status.HTTP_200_OK)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 768, in data
    ret = super().data
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 253, in data
    self._data = self.to_representation(self.instance)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 686, in to_representation
    return [

Exception Type: TypeError at /api/translate/categories/1/
Exception Value: 'TranslateCategory' object is not iterable

字符串

以下是models.py:

from django.db import models

class Translate(models.Model):
    text = models.CharField(max_length=60)
    translated_text = models.TextField(max_length=1000)

    def __str__(self):
        return self.text

class TranslateCategory(models.Model):
    title = models.CharField(max_length=60)
    items = models.ManyToManyField(Translate)

    def __str__(self):
        return self.title

serializers.py(示例网站)

from rest_framework import serializers

from .models import Translate,TranslateCategory

class TranslateSerializer(serializers.Serializer):
    text = serializers.CharField()
    translated_text = serializers.CharField()
    
    class Meta:
        model = Translate
        fields = '__all__'

class TranslateCategorySerializer(serializers.Serializer):
    id = serializers.IntegerField()
    title = serializers.CharField()
    items = TranslateSerializer(many=True)
    
    class Meta:
        model = TranslateCategory
        fields = '__all__'

views.py(示例网站)

from django.shortcuts import render
from .models import Translate , TranslateCategory
from rest_framework.response import Response
from rest_framework import permissions, status
from rest_framework.decorators import api_view, permission_classes
from .serializers import TranslateSerializer,TranslateCategorySerializer

@api_view(['GET'])
def categories(request):
    try:
        translate_category = TranslateCategory.objects.all()
    except:
        return Response("Данной категории не существует",status=status.HTTP_400_BAD_REQUEST)
    
    serializer = TranslateSerializer(translate_category,many=True)

    return Response(serializer.data ,status=status.HTTP_200_OK)

@api_view(['GET'])
def categories_id(request,id):
    try:
        translate_category = TranslateCategory.objects.get(id = id)
    except:
        return Response("Данной категории не существует",status=status.HTTP_400_BAD_REQUEST)
    
    serializer = TranslateSerializer(translate_category,many=True)

    return Response(serializer.data ,status=status.HTTP_200_OK)

@api_view(['GET'])
def translate(request):
    try:
        translate_categories = TranslateCategory.objects.all()
    except:
        return Response("Переводов нет",status=status.HTTP_400_BAD_REQUEST)

    serializer = TranslateCategorySerializer(translate_categories,many=True)

    return Response(serializer.data,status=status.HTTP_200_OK)


为了调试,我试图将views.py编辑为:

from django.shortcuts import render
from .models import Translate , TranslateCategory
from rest_framework.response import Response
from rest_framework import permissions, status
from rest_framework.decorators import api_view, permission_classes
from .serializers import TranslateSerializer,TranslateCategorySerializer

@api_view(['GET'])
def categories(request):
    try:
        translate_category = TranslateCategory.objects.all()
    except:
        return Response("Данной категории не существует",status=status.HTTP_400_BAD_REQUEST)
    
    serializer = TranslateSerializer(translate_category,many=True)

    return Response(serializer.data ,status=status.HTTP_200_OK)

@api_view(['GET'])
def categories_id(request,id):
    return Response("text" ,status=status.HTTP_200_OK)

@api_view(['GET'])
def translate(request):
    try:
        translate_categories = TranslateCategory.objects.all()
    except:
        return Response("Переводов нет",status=status.HTTP_400_BAD_REQUEST)

    serializer = TranslateCategorySerializer(translate_categories,many=True)

    return Response(serializer.data,status=status.HTTP_200_OK)


即使我保存了这个,仍然有一个错误'TranslateCategory' object is not iterable。
回溯:

Request Method: GET
Request URL: http://oralbekov.dias19.fvds.ru/api/translate/categories/1/

Django Version: 3.2.16
Python Version: 3.8.10
Installed Applications:
['jazzmin',
 'modeltranslation',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'rest_framework_simplejwt',
 'questions.apps.QuestionsConfig',
 'calendartodo.apps.CalendartodoConfig',
 'rites.apps.RitesConfig',
 'map.apps.MapConfig',
 'news.apps.NewsConfig',
 'translate.apps.TranslateConfig',
 'info.apps.InfoConfig',
 'reportederrors.apps.ReportederrorsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback (most recent call last):
  File "/var/www/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/var/www/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/var/www/hajjum/translate/views.py", line 30, in categories_id
    return Response("Переводов нет",status=status.HTTP_400_BAD_REQUEST)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 768, in data
    ret = super().data
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 253, in data
    self._data = self.to_representation(self.instance)
  File "/var/www/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 686, in to_representation
    return [

Exception Type: TypeError at /api/translate/categories/1/
Exception Value: 'TranslateCategory' object is not iterable


我也试图删除许多=True,但错误继续显示

vkc1a9a2

vkc1a9a21#

只是在你的细节视图上使用了错误的serializer。我还想补充一下,你可以通过使用Django的get_object_or_404快捷功能来进一步改进你的代码:

@api_view(["GET"])
def categories_id(request, id):
    obj = get_object_or_404(TranslateCategory, pk=id)
    serializer = TranslateCategorySerializer(obj)
    return Response(serializer.data, status=status.HTTP_200_OK)

字符串
此外,您正在以“错误”(冗余字段)的方式使用序列化器。当定义Meta时,请使用ModelSerializer

class TranslateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Translate
        fields = "__all__"

class TranslateCategorySerializer(serializers.ModelSerializer):
    items = TranslateSerializer(many=True)

    class Meta:
        model = TranslateCategory
        fields = "__all__"

相关问题