尝试获取序列化程序字段的值时出现 AttributeError

AttributeError when attempting to get a value for field on serializer

尝试在 Django 中建立 manytomany 关系,但出现以下错误 -

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

这里有一个类似的答案建议设置 many=True,但它不起作用 -

models.py

class Genre(models.Model):
  name = models.CharField(max_length=255,unique=True)

  def __unicode__(self):
    return self.name

class Movie(models.Model):
  mname = models.CharField(max_length=255)
  genres = models.ManyToManyField(Genre,related_name='movies')

  def __unicode__(self):
    return self.mname

serializers.py

class GenreSerializer(serializers.ModelSerializer):
  class Meta:
    model = Genre
    fields = ('name','id')

class MovieSerializer(serializers.ModelSerializer):
  genres = GenreSerializer(many=True, read_only=True)    

  class Meta:
    model = Movie
    fields = ('id','genres','mname')

urls.py

urlpatterns = [
url(r'^genres/$', views.GenreList.as_view()),
url(r'^genres/(?P<pk>[0-9]+)$', views.GenreDetail.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)

views.py

class GenreList(generics.ListCreateAPIView):
  queryset = Genre.objects.all()
  serializer_class = GenreSerializer


class GenreDetail(generics.RetrieveUpdateDestroyAPIView):
  serializer_class = GenreSerializer

  def get_queryset(self):
    genres = Genre.objects.get(pk=self.kwargs.get('pk', None))
    movies = Movie.objects.filter(genres=genres)
    return movies

与您的其他问题类似,您在 GenreDetail 视图中使用了不正确的序列化程序。您需要为从 get_queryset:

返回的模型类型添加序列化程序
class GenreDetail(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = MovieSerializer

    def get_queryset(self):
        genres = Genre.objects.get(pk=self.kwargs.get('pk', None))
        movies = Movie.objects.filter(genres=genres)
        return movies

如果您正在使用基于函数的序列化器,并且想要获取 ModelSerializer 的所有信息。然后使用 many=True

@api_view()
def movie_list(request):

    movies = Movie.objects.all()
    serializer = MovieSerializer(movies)
    print(serializer)
    return Response(serializer.data)

对于上面的代码,响应是

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

如果我们只是在 serializer = MovieSerializer(movies, many=True) 中添加 many=True

@api_view()
def movie_list(request):

    movies = Movie.objects.all()
    serializer = MovieSerializer(movies, many=True)
    print(serializer)
    return Response(serializer.data)

那么我们得到的输出是

让我们举一个简单的示例模型,其中有三个字段

from django.db import models

class Movie(models):
  name = models.CharField()
  description = models.TextField()
  created = models.DateTimeField(auto_now_add=True)

如果您在序列化器字段中使用它,那么您必须传递参数“many=true”以及模型名称,例如

movie = Movie.objects.all()
serializer = MovieSerializer(movie, many=True)

这是因为当你的模型中有多个对象(字段)并且你想遍历我们使用“many=True”的每个字段时