event.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from django.shortcuts import get_object_or_404
  2. from rest_framework import viewsets, permissions
  3. from rest_framework.response import Response
  4. from webservice_running.models import Event
  5. from webservice_running.serializers import EventSerializer, EventCreateSerializer
  6. class EventMVS(viewsets.ModelViewSet):
  7. queryset = Event.objects.all()
  8. serializer_class = EventSerializer
  9. #permission_classes = [IsOwnerOrReadOnly]
  10. permission_classes = [permissions.IsAuthenticated]
  11. def get_object(self):
  12. """
  13. Returns the object the view is displaying.
  14. You may want to override this if you need to provide non-standard
  15. queryset lookups. Eg if objects are referenced using multiple
  16. keyword arguments in the url conf.
  17. """
  18. # queryset = self.filter_queryset(self.get_queryset())
  19. queryset = Event.objects.select_related('order_synced__ticket')
  20. # Perform the lookup filtering.
  21. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
  22. assert lookup_url_kwarg in self.kwargs, (
  23. 'Expected view %s to be called with a URL keyword argument '
  24. 'named "%s". Fix your URL conf, or set the `.lookup_field` '
  25. 'attribute on the view correctly.' %
  26. (self.__class__.__name__, lookup_url_kwarg)
  27. )
  28. filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
  29. obj = get_object_or_404(queryset, **filter_kwargs)
  30. # May raise a permission denied
  31. self.check_object_permissions(self.request, obj)
  32. return obj
  33. def update(self, request, *args, **kwargs):
  34. partial = kwargs.pop('partial', False)
  35. instance = self.get_object()# Event.objects.select_related('order_synced__ticket').get(id=1)
  36. serializer = self.get_serializer(instance, data=request.data, partial=partial)
  37. # for serializer in serializers:
  38. serializer.is_valid(raise_exception=True)
  39. self.perform_update(serializer)
  40. if getattr(instance, '_prefetched_objects_cache', None):
  41. # If 'prefetch_related' has been applied to a queryset, we need to
  42. # forcibly invalidate the prefetch cache on the instance.
  43. instance._prefetched_objects_cache = {}
  44. return Response(serializer.data)