diff --git a/core/apps/orders/serializers.py b/core/apps/orders/serializers.py index b6591c7..c5da176 100644 --- a/core/apps/orders/serializers.py +++ b/core/apps/orders/serializers.py @@ -4,9 +4,24 @@ from core.apps.orders.models import Order class OrderListSerializer(serializers.ModelSerializer): + location_to = serializers.SerializerMethodField(method_name='get_location_to') + location_from = serializers.SerializerMethodField(method_name='get_location_from') + class Meta: model = Order fields = [ 'id', 'order_number', 'name', 'date', 'status', 'size', 'total_price', 'is_paid', 'location', 'location_to', 'location_from' - ] \ No newline at end of file + ] + + def get_location_to(self, obj): + return { + 'id': obj.location_to.id, + 'name': obj.location_to.name + } + + def get_location_from(self, obj): + return { + 'id': obj.location_from.id, + 'name': obj.location_from.name + } \ No newline at end of file diff --git a/core/apps/orders/views.py b/core/apps/orders/views.py index 0dae399..cb54f6a 100644 --- a/core/apps/orders/views.py +++ b/core/apps/orders/views.py @@ -6,7 +6,7 @@ from core.apps.orders import serializers, models class OrderListApiView(generics.GenericAPIView): serializer_class = serializers.OrderListSerializer - queryset = models.Order.objects.all() + queryset = models.Order.objects.select_related('location_to', 'location_from') permission_classes = [permissions.IsAuthenticated] def get(self, request):