gpt4 book ai didi

python - aggregate(Max ('id' )) 返回异常 'str' 对象没有属性 'email'

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:15 24 4
gpt4 key购买 nike

我一直在尝试获取具有最高 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')

View :

class GetHighestValue(generics.ListAPIView):
serializer_class = UserSerializer

def get_queryset(self):
return User.objects.aggregate(Max('id'))

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'.

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/

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