我一直在尝试让用户获得最高ID,但没有成功。
这是我的用户模型:
class User(models.Model):
email=models.EmailField(unique=True, null=False)
name=models.TextField(null=True)
它的序列化器:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'email', 'name')
风景:
class GetHighestValue(generics.ListAPIView):
serializer_class = UserSerializer
def get_queryset(self):
return User.objects.aggregate(Max('id'))
尝试获取字段
email
的值时出现AttributeError序列化器
UserSerializer
。序列化程序字段可能命名为错误且与
str
实例上的任何属性或键都不匹配。原始异常文本为:'str'对象没有属性'email'。
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/generics.py", line 201, in get
return self.list(request, *args, **kwargs)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/mixins.py", line 48, in list
return Response(serializer.data)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/serializers.py", line 674, in data
ret = super(ListSerializer, self).data
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/serializers.py", line 239, in data
self._data = self.to_representation(self.instance)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/serializers.py", line 614, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/serializers.py", line 463, in to_representation
attribute = field.get_attribute(instance)
File "/home/user/.local/lib/python2.7/site-packages/rest_framework/fields.py", line 422, in get_attribute
raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `email` on serializer `UserSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `str` instance.
Original exception text was: 'str' object has no attribute 'email'.
最佳答案
问题在这里
def get_queryset(self):
return User.objects.aggregate(Max('id'))
期望的返回值是一个查询集。但是聚合不返回查询集。使用User.objects.get()也不返回查询集。返回查询集的唯一方法是使用
all()
或filter()
def get_queryset(self):
return User.objects.order_by('-id')[:1]
这里暗含all(),[:1]确保您返回的是一个包含对象而不是单个对象的可迭代对象。
关于python - aggregate(Max('id'))返回异常'str'对象没有属性'email',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37449532/