Django在APIView中获取ForeignKey对象

时间:2018-09-16 23:46:34

标签: django python-3.x django-models django-rest-framework django-views

嘿,所以我是Django的新手,我正在使用以下模型编写REST API。

# models.py
class Order(models.Model):
    order_name = models.CharField(max_length=10, unique=True, default="")

    def __str__(self):
        return '{0}'.format(self.order_name).


class LineItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)

    class Meta:
        unique_together = ('order', 'product')

    def __str__(self):
        return '{} - {}'.format(self.order.order_name, self.product.product_name).


# views.py
class OrderList(APIView):
    def get(self, request):
        orderlist = Order.objects.all()
        serializer = OrderSerializer(orderlist, many=True)
        return Response(serializer.data)

    def post(self, request):
        serializer = OrderSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class OrderDetail(APIView):
    def get_object(self, order):
        try:
            return Order.objects.get(order_name=order_name)
        except Order.DoesNotExist:
            raise Http404

    def get(self, request, order_name):
        snippet = Order.objects.get(order_name=order_name)
        snippet = snippet.lineitem_set.all()
        serializer = OrderSerializer(snippet)
        return Response(serializer.data)

因此,我尝试对OrderDetail(APIView)的get方法进行编码,以便在/ api / order / OrderA /中获得具有该顺序中所有行项的JSON。我已经为此努力了一段时间。有什么建议吗?

class OrderSerializer(serializers.ModelSerializer): class Meta: model = Order fields = ['order_name']

2 个答案:

答案 0 :(得分:0)

DRF提供了一个类来获取对象的详细信息。在这里看看:http://www.django-rest-framework.org/api-guide/generic-views/#retrieveapiview

这是一个示例代码,如果使用GET方法将order_name作为参数传递到url中,则该代码应该起作用。

from django.views.decorators.cache import never_cache
from django.utils.decorators import method_decorator
from rest_framework.exceptions import NotFound
from rest_framework.generics import RetrieveAPIView

# the decorator avoid to cache the object, maybe you don't need that.
@method_decorator(never_cache, name="dispatch")
class OrderDetail(RetrieveAPIView):

    serializer_class = OrderSerializer

    def get_object(self):
        try:
            return Order.objects.get(order_name=self.kwargs.get('order_name'))
        except Order.DoesNotExist:
            raise NotFound()

答案 1 :(得分:0)

如下更改您的序列化器,

class LineItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = LineItem
        fields = ('id', 'product')
        depth = 1


class OrderSerializer(serializers.ModelSerializer):
    line_items = LineItemSerializer(many=True, source='lineitem_set')

    class Meta:
        model = Order
        fields = ['order_name', 'line_items']

,然后在您的 OrderDetail 视图中以

class OrderDetail(APIView):
    # your code

    def get(self, request, order_name):
        snippet = Order.objects.get(order_name=order_name)
        serializer = OrderSerializer(snippet)
        return Response(serializer.data)