Meepo Meepo - 6 months ago 13
Python Question

aggregate(Max('id')) returns exception 'str' object has no attribute 'email'

I've been trying to get the user with the highest id but no success. :/
This is my user model:

class User(models.Model):
email=models.EmailField(unique=True, null=False)
name=models.TextField(null=True)


It's serializer:

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'email', 'name')


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


I've tried many things but nothing works.
Any help would be grateful.
Traceback:


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

Answer

The problem is here

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

The expected return value is a queryset. But aggregate does not return a queryset. Using User.objects.get() does not return a queryset either. The only way to return a queryset, is to use all() or filter()

def get_queryset(self):
    return User.objects.order_by(-'id')[0:1]

The all() is implied here and [0:1] ensures that you are return an iterable rather than a single object.