Reputation: 73
I'm building a API to get some informations from a Chess board in Django, I have a model with fields: id, piece_name, color, initial_position.
MODEL
class ChessB(models.Model):
class Meta:
db_table = 'chess'
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
piece_name = models.CharField(max_length=200)
color = models.CharField(max_length=200)
initial_position = models.CharField(max_length=200)
SERIALIZERS
class ChessBSerializer(serializers.ModelSerializer):
class Meta:
model = ChessB
fields = '__all__'
VIEW
class ChessBList(generics.ListCreateAPIView):
serializer_class = ChessBSerializer
def get_queryset(self):
queryset = ChessB.objects.all()
piece_name = self.request.query_params.get('piece_name')
if piece_name is not None:
queryset = queryset.filter(piece_name=piece_name)
queryset = queryset.values_list('id', flat=True)
return queryset
What i'm trying to do is: informing my piece_name as paramether, should return just my Id
For example: call http://127.0.0.1:8000/chessb/?piece_name=queen should return:
{
"id":"id-from-queen-here"
}
But when I tried to use values_list('id') on my View, I got a error as follow:
Internal Server Error: /chessb/
Traceback (most recent call last):
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\fields.py", line 457, in get_attribute
return get_attribute(instance, self.source_attrs)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\fields.py", line 97, in get_attribute
instance = getattr(instance, attr)
AttributeError: 'UUID' object has no attribute 'piece_name'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\luizg\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\luizg\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\luizg\Python\Python310\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\luizg\Python\Python310\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\generics.py", line 239, in get
return self.list(request, *args, **kwargs)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\mixins.py", line 46, in list
return Response(serializer.data)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 745, in data
ret = super().data
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 246, in data
self._data = self.to_representation(self.instance)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 663, in to_representation
return [
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 664, in <listcomp>
self.child.to_representation(item) for item in iterable
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\serializers.py", line 502, in to_representation
attribute = field.get_attribute(instance)
File "C:\Users\luizg\Python\Python310\lib\site-packages\rest_framework\fields.py", line 490, in get_attribute
raise type(exc)(msg)
AttributeError: Got AttributeError when attempting to get a value for field `piece_name` on serializer `ChessBSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `UUID` instance.
Original exception text was: 'UUID' object has no attribute 'piece_name'.
What is my error here?
Upvotes: 1
Views: 164
Reputation:
get_queryset
should return a Queryset… hence the name. You are returning a list of ids which will not work. If you want to specify a certain set of fields to return, that should go in the serializer.
class ChessBSerializer(serializers.ModelSerializer):
class Meta:
model = ChessB
fields = ("id",)
Upvotes: 1