prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Food API: AttributeError: 'Event' object has no attribute 'member_registration'
  Sentry Issue: [CONCREXIT-KG](https://sentry.io/organizations/thalia/issues/3768057031/?referrer=github_integration)
  
  ```
  AttributeError: 'Event' object has no attribute 'member_registration'
  (11 additional frame(s) were not displayed)
  ...
    File "rest_framework/serializers.py", line 253, in data
      self._data = self.to_representation(self.instance)
    File "rest_framework/serializers.py", line 522, in to_representation
      ret[field.field_name] = field.to_representation(attribute)
    File "rest_framework/serializers.py", line 522, in to_representation
      ret[field.field_name] = field.to_representation(attribute)
    File "rest_framework/fields.py", line 1838, in to_representation
      return method(value)
    File "events/api/v2/serializers/event.py", line 83, in _registration_status
      if self.context["request"].member and len(instance.member_registration) > 0:
  ```
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `website/pizzas/api/v2/views.py`
  Content:
  ```
  1 from oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope
  2 from rest_framework import filters as framework_filters
  3 from rest_framework import status
  4 from rest_framework.generics import (
  5     CreateAPIView,
  6     DestroyAPIView,
  7     ListAPIView,
  8     RetrieveAPIView,
  9     UpdateAPIView,
  10     get_object_or_404,
  11 )
  12 from rest_framework.response import Response
  13 
  14 from payments.exceptions import PaymentError
  15 from payments.services import delete_payment
  16 from pizzas.api.v2 import filters
  17 from pizzas.api.v2.serializers import (
  18     FoodOrderCreateSerializer,
  19     FoodOrderSerializer,
  20     FoodOrderUpdateSerializer,
  21     ProductSerializer,
  22 )
  23 from pizzas.api.v2.serializers.food_event import FoodEventSerializer
  24 from pizzas.models import FoodEvent, FoodOrder, Product
  25 from thaliawebsite.api.v2.permissions import IsAuthenticatedOrTokenHasScopeForMethod
  26 
  27 
  28 class FoodEventListView(ListAPIView):
  29     """Returns an overview of all food events."""
  30 
  31     serializer_class = FoodEventSerializer
  32     queryset = FoodEvent.objects.all()
  33     filter_backends = (
  34         framework_filters.OrderingFilter,
  35         filters.FoodEventDateFilterBackend,
  36     )
  37     ordering_fields = ("start", "end")
  38     permission_classes = [
  39         IsAuthenticatedOrTokenHasScope,
  40     ]
  41     required_scopes = ["food:read"]
  42 
  43 
  44 class FoodEventDetailView(RetrieveAPIView):
  45     """Returns one single food event."""
  46 
  47     serializer_class = FoodEventSerializer
  48     queryset = FoodEvent.objects.all()
  49     permission_classes = [
  50         IsAuthenticatedOrTokenHasScope,
  51     ]
  52     required_scopes = ["food:read"]
  53 
  54 
  55 class FoodEventProductsListView(ListAPIView):
  56     """Returns an overview of all products."""
  57 
  58     serializer_class = ProductSerializer
  59     queryset = Product.available_products.all()
  60     filter_backends = (framework_filters.SearchFilter,)
  61     search_fields = ("name",)
  62     permission_classes = [
  63         IsAuthenticatedOrTokenHasScope,
  64     ]
  65     required_scopes = ["food:read"]
  66 
  67 
  68 class FoodEventOrderDetailView(
  69     RetrieveAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
  70 ):
  71     """Returns details of a food order."""
  72 
  73     permission_classes = [
  74         IsAuthenticatedOrTokenHasScopeForMethod,
  75     ]
  76     required_scopes_per_method = {
  77         "GET": ["food:read"],
  78         "POST": ["food:order"],
  79         "PUT": ["food:order"],
  80         "PATCH": ["food:order"],
  81         "DELETE": ["food:order"],
  82     }
  83 
  84     def get_serializer_class(self):
  85         if self.request.method.lower() == "get":
  86             return FoodOrderSerializer
  87         if self.request.method.lower() == "post":
  88             return FoodOrderCreateSerializer
  89         return FoodOrderUpdateSerializer
  90 
  91     def get_queryset(self):
  92         return FoodOrder.objects.filter(food_event=self.food_event)
  93 
  94     def get_object(self):
  95         queryset = self.filter_queryset(self.get_queryset())
  96         obj = get_object_or_404(queryset, member=self.request.member)
  97 
  98         # May raise a permission denied
  99         self.check_object_permissions(self.request, obj)
  100 
  101         return obj
  102 
  103     def dispatch(self, request, *args, **kwargs):
  104         self.food_event = get_object_or_404(FoodEvent, pk=self.kwargs.get("pk"))
  105         try:
  106             return super().dispatch(request, *args, **kwargs)
  107         except PaymentError as e:
  108             return Response(
  109                 str(e),
  110                 status=status.HTTP_403_FORBIDDEN,
  111             )
  112 
  113     def update(self, request, *args, **kwargs):
  114         instance = self.get_object()
  115 
  116         if instance.payment:
  117             delete_payment(instance, member=request.member, ignore_change_window=True)
  118 
  119         super().update(request, *args, **kwargs)
  120 
  121         return Response(
  122             FoodOrderSerializer(instance, context=self.get_serializer_context()).data
  123         )
  124 
  125     def create(self, request, *args, **kwargs):
  126         serializer = self.get_serializer(data=request.data)
  127         serializer.is_valid(raise_exception=True)
  128         self.perform_create(serializer)
  129         return Response(
  130             FoodOrderSerializer(
  131                 serializer.instance, context=self.get_serializer_context()
  132             ).data,
  133             status=status.HTTP_201_CREATED,
  134         )
  135
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/website/pizzas/api/v2/views.py b/website/pizzas/api/v2/views.py
  --- a/website/pizzas/api/v2/views.py
  +++ b/website/pizzas/api/v2/views.py
  @@ -1,3 +1,5 @@
  +from django.db.models import Prefetch
  +
   from oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope
   from rest_framework import filters as framework_filters
   from rest_framework import status
  @@ -11,6 +13,7 @@
   )
   from rest_framework.response import Response
   
  +from events.models.event_registration import EventRegistration
   from payments.exceptions import PaymentError
   from payments.services import delete_payment
   from pizzas.api.v2 import filters
  @@ -45,12 +48,25 @@
       """Returns one single food event."""
   
       serializer_class = FoodEventSerializer
  -    queryset = FoodEvent.objects.all()
       permission_classes = [
           IsAuthenticatedOrTokenHasScope,
       ]
       required_scopes = ["food:read"]
   
  +    def get_queryset(self):
  +        events = FoodEvent.objects.all()
  +        if self.request.member:
  +            events = events.prefetch_related(
  +                Prefetch(
  +                    "event__eventregistration_set",
  +                    to_attr="member_registration",
  +                    queryset=EventRegistration.objects.filter(
  +                        member=self.request.member
  +                    ).select_properties("queue_position"),
  +                )
  +            )
  +        return events
  +
   
   class FoodEventProductsListView(ListAPIView):
       """Returns an overview of all products."""
  
verification_info: {"golden_diff": "diff --git a/website/pizzas/api/v2/views.py b/website/pizzas/api/v2/views.py\n--- a/website/pizzas/api/v2/views.py\n+++ b/website/pizzas/api/v2/views.py\n@@ -1,3 +1,5 @@\n+from django.db.models import Prefetch\n+\n from oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope\n from rest_framework import filters as framework_filters\n from rest_framework import status\n@@ -11,6 +13,7 @@\n )\n from rest_framework.response import Response\n \n+from events.models.event_registration import EventRegistration\n from payments.exceptions import PaymentError\n from payments.services import delete_payment\n from pizzas.api.v2 import filters\n@@ -45,12 +48,25 @@\n     \"\"\"Returns one single food event.\"\"\"\n \n     serializer_class = FoodEventSerializer\n-    queryset = FoodEvent.objects.all()\n     permission_classes = [\n         IsAuthenticatedOrTokenHasScope,\n     ]\n     required_scopes = [\"food:read\"]\n \n+    def get_queryset(self):\n+        events = FoodEvent.objects.all()\n+        if self.request.member:\n+            events = events.prefetch_related(\n+                Prefetch(\n+                    \"event__eventregistration_set\",\n+                    to_attr=\"member_registration\",\n+                    queryset=EventRegistration.objects.filter(\n+                        member=self.request.member\n+                    ).select_properties(\"queue_position\"),\n+                )\n+            )\n+        return events\n+\n \n class FoodEventProductsListView(ListAPIView):\n     \"\"\"Returns an overview of all products.\"\"\"\n", "issue": "Food API: AttributeError: 'Event' object has no attribute 'member_registration'\nSentry Issue: [CONCREXIT-KG](https://sentry.io/organizations/thalia/issues/3768057031/?referrer=github_integration)\n\n```\nAttributeError: 'Event' object has no attribute 'member_registration'\n(11 additional frame(s) were not displayed)\n...\n  File \"rest_framework/serializers.py\", line 253, in data\n    self._data = self.to_representation(self.instance)\n  File \"rest_framework/serializers.py\", line 522, in to_representation\n    ret[field.field_name] = field.to_representation(attribute)\n  File \"rest_framework/serializers.py\", line 522, in to_representation\n    ret[field.field_name] = field.to_representation(attribute)\n  File \"rest_framework/fields.py\", line 1838, in to_representation\n    return method(value)\n  File \"events/api/v2/serializers/event.py\", line 83, in _registration_status\n    if self.context[\"request\"].member and len(instance.member_registration) > 0:\n```\n", "before_files": [{"content": "from oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope\nfrom rest_framework import filters as framework_filters\nfrom rest_framework import status\nfrom rest_framework.generics import (\n    CreateAPIView,\n    DestroyAPIView,\n    ListAPIView,\n    RetrieveAPIView,\n    UpdateAPIView,\n    get_object_or_404,\n)\nfrom rest_framework.response import Response\n\nfrom payments.exceptions import PaymentError\nfrom payments.services import delete_payment\nfrom pizzas.api.v2 import filters\nfrom pizzas.api.v2.serializers import (\n    FoodOrderCreateSerializer,\n    FoodOrderSerializer,\n    FoodOrderUpdateSerializer,\n    ProductSerializer,\n)\nfrom pizzas.api.v2.serializers.food_event import FoodEventSerializer\nfrom pizzas.models import FoodEvent, FoodOrder, Product\nfrom thaliawebsite.api.v2.permissions import IsAuthenticatedOrTokenHasScopeForMethod\n\n\nclass FoodEventListView(ListAPIView):\n    \"\"\"Returns an overview of all food events.\"\"\"\n\n    serializer_class = FoodEventSerializer\n    queryset = FoodEvent.objects.all()\n    filter_backends = (\n        framework_filters.OrderingFilter,\n        filters.FoodEventDateFilterBackend,\n    )\n    ordering_fields = (\"start\", \"end\")\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScope,\n    ]\n    required_scopes = [\"food:read\"]\n\n\nclass FoodEventDetailView(RetrieveAPIView):\n    \"\"\"Returns one single food event.\"\"\"\n\n    serializer_class = FoodEventSerializer\n    queryset = FoodEvent.objects.all()\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScope,\n    ]\n    required_scopes = [\"food:read\"]\n\n\nclass FoodEventProductsListView(ListAPIView):\n    \"\"\"Returns an overview of all products.\"\"\"\n\n    serializer_class = ProductSerializer\n    queryset = Product.available_products.all()\n    filter_backends = (framework_filters.SearchFilter,)\n    search_fields = (\"name\",)\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScope,\n    ]\n    required_scopes = [\"food:read\"]\n\n\nclass FoodEventOrderDetailView(\n    RetrieveAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView\n):\n    \"\"\"Returns details of a food order.\"\"\"\n\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScopeForMethod,\n    ]\n    required_scopes_per_method = {\n        \"GET\": [\"food:read\"],\n        \"POST\": [\"food:order\"],\n        \"PUT\": [\"food:order\"],\n        \"PATCH\": [\"food:order\"],\n        \"DELETE\": [\"food:order\"],\n    }\n\n    def get_serializer_class(self):\n        if self.request.method.lower() == \"get\":\n            return FoodOrderSerializer\n        if self.request.method.lower() == \"post\":\n            return FoodOrderCreateSerializer\n        return FoodOrderUpdateSerializer\n\n    def get_queryset(self):\n        return FoodOrder.objects.filter(food_event=self.food_event)\n\n    def get_object(self):\n        queryset = self.filter_queryset(self.get_queryset())\n        obj = get_object_or_404(queryset, member=self.request.member)\n\n        # May raise a permission denied\n        self.check_object_permissions(self.request, obj)\n\n        return obj\n\n    def dispatch(self, request, *args, **kwargs):\n        self.food_event = get_object_or_404(FoodEvent, pk=self.kwargs.get(\"pk\"))\n        try:\n            return super().dispatch(request, *args, **kwargs)\n        except PaymentError as e:\n            return Response(\n                str(e),\n                status=status.HTTP_403_FORBIDDEN,\n            )\n\n    def update(self, request, *args, **kwargs):\n        instance = self.get_object()\n\n        if instance.payment:\n            delete_payment(instance, member=request.member, ignore_change_window=True)\n\n        super().update(request, *args, **kwargs)\n\n        return Response(\n            FoodOrderSerializer(instance, context=self.get_serializer_context()).data\n        )\n\n    def create(self, request, *args, **kwargs):\n        serializer = self.get_serializer(data=request.data)\n        serializer.is_valid(raise_exception=True)\n        self.perform_create(serializer)\n        return Response(\n            FoodOrderSerializer(\n                serializer.instance, context=self.get_serializer_context()\n            ).data,\n            status=status.HTTP_201_CREATED,\n        )\n", "path": "website/pizzas/api/v2/views.py"}], "after_files": [{"content": "from django.db.models import Prefetch\n\nfrom oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope\nfrom rest_framework import filters as framework_filters\nfrom rest_framework import status\nfrom rest_framework.generics import (\n    CreateAPIView,\n    DestroyAPIView,\n    ListAPIView,\n    RetrieveAPIView,\n    UpdateAPIView,\n    get_object_or_404,\n)\nfrom rest_framework.response import Response\n\nfrom events.models.event_registration import EventRegistration\nfrom payments.exceptions import PaymentError\nfrom payments.services import delete_payment\nfrom pizzas.api.v2 import filters\nfrom pizzas.api.v2.serializers import (\n    FoodOrderCreateSerializer,\n    FoodOrderSerializer,\n    FoodOrderUpdateSerializer,\n    ProductSerializer,\n)\nfrom pizzas.api.v2.serializers.food_event import FoodEventSerializer\nfrom pizzas.models import FoodEvent, FoodOrder, Product\nfrom thaliawebsite.api.v2.permissions import IsAuthenticatedOrTokenHasScopeForMethod\n\n\nclass FoodEventListView(ListAPIView):\n    \"\"\"Returns an overview of all food events.\"\"\"\n\n    serializer_class = FoodEventSerializer\n    queryset = FoodEvent.objects.all()\n    filter_backends = (\n        framework_filters.OrderingFilter,\n        filters.FoodEventDateFilterBackend,\n    )\n    ordering_fields = (\"start\", \"end\")\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScope,\n    ]\n    required_scopes = [\"food:read\"]\n\n\nclass FoodEventDetailView(RetrieveAPIView):\n    \"\"\"Returns one single food event.\"\"\"\n\n    serializer_class = FoodEventSerializer\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScope,\n    ]\n    required_scopes = [\"food:read\"]\n\n    def get_queryset(self):\n        events = FoodEvent.objects.all()\n        if self.request.member:\n            events = events.prefetch_related(\n                Prefetch(\n                    \"event__eventregistration_set\",\n                    to_attr=\"member_registration\",\n                    queryset=EventRegistration.objects.filter(\n                        member=self.request.member\n                    ).select_properties(\"queue_position\"),\n                )\n            )\n        return events\n\n\nclass FoodEventProductsListView(ListAPIView):\n    \"\"\"Returns an overview of all products.\"\"\"\n\n    serializer_class = ProductSerializer\n    queryset = Product.available_products.all()\n    filter_backends = (framework_filters.SearchFilter,)\n    search_fields = (\"name\",)\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScope,\n    ]\n    required_scopes = [\"food:read\"]\n\n\nclass FoodEventOrderDetailView(\n    RetrieveAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView\n):\n    \"\"\"Returns details of a food order.\"\"\"\n\n    permission_classes = [\n        IsAuthenticatedOrTokenHasScopeForMethod,\n    ]\n    required_scopes_per_method = {\n        \"GET\": [\"food:read\"],\n        \"POST\": [\"food:order\"],\n        \"PUT\": [\"food:order\"],\n        \"PATCH\": [\"food:order\"],\n        \"DELETE\": [\"food:order\"],\n    }\n\n    def get_serializer_class(self):\n        if self.request.method.lower() == \"get\":\n            return FoodOrderSerializer\n        if self.request.method.lower() == \"post\":\n            return FoodOrderCreateSerializer\n        return FoodOrderUpdateSerializer\n\n    def get_queryset(self):\n        return FoodOrder.objects.filter(food_event=self.food_event)\n\n    def get_object(self):\n        queryset = self.filter_queryset(self.get_queryset())\n        obj = get_object_or_404(queryset, member=self.request.member)\n\n        # May raise a permission denied\n        self.check_object_permissions(self.request, obj)\n\n        return obj\n\n    def dispatch(self, request, *args, **kwargs):\n        self.food_event = get_object_or_404(FoodEvent, pk=self.kwargs.get(\"pk\"))\n        try:\n            return super().dispatch(request, *args, **kwargs)\n        except PaymentError as e:\n            return Response(\n                str(e),\n                status=status.HTTP_403_FORBIDDEN,\n            )\n\n    def update(self, request, *args, **kwargs):\n        instance = self.get_object()\n\n        if instance.payment:\n            delete_payment(instance, member=request.member, ignore_change_window=True)\n\n        super().update(request, *args, **kwargs)\n\n        return Response(\n            FoodOrderSerializer(instance, context=self.get_serializer_context()).data\n        )\n\n    def create(self, request, *args, **kwargs):\n        serializer = self.get_serializer(data=request.data)\n        serializer.is_valid(raise_exception=True)\n        self.perform_create(serializer)\n        return Response(\n            FoodOrderSerializer(\n                serializer.instance, context=self.get_serializer_context()\n            ).data,\n            status=status.HTTP_201_CREATED,\n        )\n", "path": "website/pizzas/api/v2/views.py"}]}