Skip to content
Advertisement

Django rest framework’s StringRelatedField is throwing KeyError

I have the following model classes.

class Categories(models.Model):
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, unique=True)
    business = models.ForeignKey(Business, related_name='category_business', on_delete=models.CASCADE)
    name = models.CharField(max_length=128)

    class Meta:
        unique_together = ('business', 'name')


class Menu(models.Model):
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, unique=True)
    business = models.ForeignKey(Business, related_name='menu_business', on_delete=models.CASCADE)
    name = models.CharField(max_length=128)
    description = models.CharField(max_length=128)
    category = models.ForeignKey(Categories, related_name='menus', on_delete=models.CASCADE)
    price = models.IntegerField()

    class Meta:
        unique_together = ('business', 'name', 'category')

    def __str__(self):
        return '%s %s %s' % (self.name, self.price, self.description)

and I have imported these classes as following as they are located in a separate package

Categories = apps.get_model('business', 'Categories')
Menu = apps.get_model('business', 'Menu')

and this is my serializer class

class GetCategoriesSerializer(serializers.ModelSerializer):
    menus = serializers.StringRelatedField(many=True)

    class Meta:
        model = Categories
        fields = ('name', 'menus')

and views is

class GetCategories(generics.ListAPIView):
    """
    Returns a list of businesses to the user. It'd read only and no authentication is needed
    """
    permission_classes = [ReadOnly]
    queryset = Categories.objects.values()
    serializer_class = GetCategoriesSerializer

and url has the following

path('customer/<str:pk>/categories', GetCategories.as_view()),

I am getting the following error

Traceback (most recent call last):
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/generics.py", line 199, in get
    return self.list(request, *args, **kwargs)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/mixins.py", line 48, in list
    return Response(serializer.data)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 760, in data
    ret = super().data
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 260, in data
    self._data = self.to_representation(self.instance)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 678, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 678, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 516, in to_representation
    attribute = field.get_attribute(instance)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/relations.py", line 529, in get_attribute
    relationship = get_attribute(instance, self.source_attrs)
  File "/Users/mymac/myapp/mytestapp/venv/lib/python3.7/site-packages/rest_framework/fields.py", line 92, in get_attribute
    instance = instance[attr]
Exception Type: KeyError at /customer/85f44d20-f936-4940-8e15-01393e42c4a8/categories
Exception Value: 'menus'

I looked into the DRF example and this seems to be a simple thing to achieve. https://www.django-rest-framework.org/api-guide/relations/#api-reference

But I am kind of stuck at this point. Am I doing anything wrong? Thanks in advance for any help.

Advertisement

Answer

You should’ve used the QuerySet instead of ValueQuerySet. That is use

queryset = Categories.objects.all()
instead of
queryset = Categories.objects.values()

So change the queryset attribute of the view as,

class GetCategories(generics.ListAPIView):
    permission_classes = [ReadOnly]
    queryset = Categories.objects.all()
    serializer_class = GetCategoriesSerializer
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement