12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- from django.shortcuts import get_object_or_404
- from rest_framework import viewsets, permissions
- from rest_framework.response import Response
- from webservice_running.models import Sportsman
- from webservice_running.serializers import SportsmanSerializer
- class SportsmanMVS(viewsets.ModelViewSet):
- serializer_class = SportsmanSerializer
- queryset = Sportsman.objects.all()
- #permission_classes = [IsOwnerOrReadOnly]
- #permission_classes = [permissions.IsAuthenticated]
- def get_queryset(self):
- """
- Optionally restricts the returned sportsmen to a given phone number,
- by filtering against a `phone_number` query parameter in the URL.
- """
- print(self.kwargs)
- queryset = Sportsman.objects.select_related('client__user')
- phone_number = self.request.query_params.get('phone_number', None)
- if phone_number is not None:
- queryset = queryset.filter(client__user__phone_number=phone_number)
- return queryset
- def get_object(self, queryset=None):
- """
- Returns the object the view is displaying.
- You may want to override this if you need to provide non-standard
- queryset lookups. Eg if objects are referenced using multiple
- keyword arguments in the url conf.
- """
- queryset = self.filter_queryset(self.get_queryset())
- # Perform the lookup filtering.
- lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
- assert lookup_url_kwarg in self.kwargs, (
- 'Expected view %s to be called with a URL keyword argument '
- 'named "%s". Fix your URL conf, or set the `.lookup_field` '
- 'attribute on the view correctly.' %
- (self.__class__.__name__, lookup_url_kwarg)
- )
- filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
- obj = get_object_or_404(queryset, **filter_kwargs)
- # May raise a permission denied
- self.check_object_permissions(self.request, obj)
- return obj
- def update(self, request, *args, **kwargs):
- partial = kwargs.pop('partial', False)
- instance = self.get_object()# Event.objects.select_related('order_synced__ticket').get(id=1)
- serializer = self.get_serializer(instance, data=request.data, partial=partial)
- # for serializer in serializers:
- serializer.is_valid(raise_exception=True)
- self.perform_update(serializer)
- if getattr(instance, '_prefetched_objects_cache', None):
- # If 'prefetch_related' has been applied to a queryset, we need to
- # forcibly invalidate the prefetch cache on the instance.
- instance._prefetched_objects_cache = {}
- return Response(serializer.data)
|