25

我在 Django REST Framework 网站的教程中找不到任何关于如何实现这一点的信息,我也没有设法在文档中找到它,尽管我确信它在某个地方。

我想issues成为父资源并成为子资源,pages以便/issues/1/pages返回所有页面issue_id为 1。

有没有一种使用基于通用类的视图来实现这一点的好方法?

这是我到目前为止所拥有的。

休息API/urls.py:

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from restAPI import views

urlpatterns = patterns('',
    url(r'^issues/$', views.IssueList.as_view()),
    url(r'^issues/(?P<pk>[0-9]+)/$', views.IssueDetail.as_view()),


    url(r'^issues/(?P<issue_id>[0-9]+)/pages/$', views.PageList.as_view()),    
    url(r'^pages/(?P<pk>[0-9]+)/$', views.PageDetail.as_view()),
)

urlpatterns = format_suffix_patterns(urlpatterns)

休息API/models.py:

from django.db import models

class Issue(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    revision = models.IntegerField(default = 1)
    issue_date = models.DateTimeField(auto_now_add=True)
    issue_image_url = models.CharField(max_length=100)

class Page(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    page_number = models.IntegerField()
    standard_page_url = models.CharField(max_length=100, default='')
    large_page_url = models.CharField(max_length=100, default='')
    thumbnail_url = models.CharField(max_length=100, default='')

    issue = models.ForeignKey(Issue, related_name="pages")

    class Meta:
        ordering = ('page_number',)

restAPI/serializers.py:

from rest_framework import serializers
from restAPI.models import Page, Issue

class IssueSerializer(serializers.ModelSerializer):
    class Meta:
        model = Issue
        fields = ('id', 'created', 'revision', 'issue_date', 'issue_image_url')

class PageSerializer(serializers.ModelSerializer):       
    class Meta:
        model = Page
        fields = ('id', 'created', 'page_number', 'standard_page_url', 'large_page_url', 'thumbnail_url')

休息API/views.py:

from restAPI.models import Page, Issue
from restAPI.serializers import PageSerializer, IssueSerializer
from rest_framework import mixins
from rest_framework import generics

class IssueList(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):
    queryset = Issue.objects.all()
    serializer_class = IssueSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

class IssueDetail(mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.DestroyModelMixin,
                    generics.GenericAPIView):
    queryset = Issue.objects.all()
    serializer_class = IssueSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)  

class PageList(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):
    queryset = Page.objects.all()
    serializer_class = PageSerializer

    def get(self, request, *args, **kwargs):
        print kwargs
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

class PageDetail(mixins.RetrieveModelMixin,
                   mixins.UpdateModelMixin,
                   mixins.DestroyModelMixin,
                   generics.GenericAPIView):
    queryset = Page.objects.all()
    serializer_class = PageSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

issues我怎样才能实现和之间的这种关系pages

4

3 回答 3

12

这是我这样做的另一种方式:

视图.py

from models import Customer, Order
from serializers import CustomerSerializer, OrderSerializer

from rest_framework import generics

class CustomerList(generics.ListCreateAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

class CustomerDetail(generics.RetrieveUpdateDestroyAPIView)
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

class OrdersByCustomer(generics.ListCreateAPIView):
    queryset = Order.objects.all()
    serializer_class = OrderSerializer

    def get_queryset(self):
        customer_pk = self.kwargs['customer_pk']
        return self.queryset.filter(customer__pk=customer_pk)

    def pre_save(self, obj):
        obj.customer_id = self.kwargs['customer_pk'] 

class OrderDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Order.objects.all()
    serializer_class = OrderSerializer

序列化程序.py

from models import Customer, Order

from rest_framework import serializers
from rest_framework.reverse import reverse

class OrderSerializer(serializers.HyperlinkedModelSerializer)

    class Meta:
        model = Order

class CustomerSerializer(serializers.HyperlinkedModelSerializer)

    orders = serializers.SerializerMethodField('get_customer_orders')

    def get_customer_orders(self, obj):
        return reverse('ordersbycustomer-list', 
               args=[obj.pk], request=self.context['request'])

    class Meta:
        model = Customer

网址.py

from django.conf.urls import patterns, include, url
from views import OrdersByCustomer, CustomerDetail, CustomerList

urlpatterns = patterns("",
    url(r'^customers/(?P<customer_pk>.+)/orders/$', OrdersByCustomer.as_view(), name='ordersbycustomer-list'),
    url(r'^customers/(?P<pk>.+)/$', CustomerDetail.as_view(), name='customer-detail'),
    url(r'^customers/$', CustomerList.as_view(), name='customer-list'),
    )

与 Viewsets/Routers 相比,涉及的代码更多,但这使您可以更好地控制正在发生的事情。

在这里,我选择仅将订单公开为客户的孩子。由于它们是分开的,因此您可以对列表和详细信息使用不同的序列化程序类。

于 2013-07-20T15:39:59.690 回答
8

以下是我使用来自 Rest-Framework 2.3 版的新ViewSetsRouters实现这一目标的方法:

视图.py:

from rest_framework import viewsets
from rest_framework.response import Response
from models import Order, OrderLine
from serializers import OrderSerializer, OrderLineSerializer

class OrderViewSet(viewsets.ModelViewSet):
    queryset = Order.objects.all()
    serializer_class = OrderSerializer

    @link()
    def lines(self, request, pk=None):
        queryset = OrderLine.objects.filter(order__pk=pk)
        serializer = OrderLineSerializer(queryset,
                           context={'request':request},
                           many=True)
        return Response(serializer.data)

class OrderLineViewSet(viewsets.ModelViewSet):
    queryset = OrderLine.objects.all()
    serializer_class = OrderLineSerializer

序列化程序.py

from rest_framework import serializers
from models import Order, OrderLine

class OrderSerializer(serializers.HyperlinkedModelSerializer):
    lines = serializers.HyperlinkedIdentityField(view_name='order-lines')

    class Meta:
        model = Order


class OrderLineSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = OrderLine

网址.py

from views import OrderViewSet, OrderLineViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'order', OrderViewSet)
router.register(r'orderline', OrderLineViewSet)
urlpatterns = router.urls

现在 'order/id/lines' 将返回与该 id 标识的订单有关系的序列化订单行列表。

当您向路由器注册视图时,任何用@link 或@action 装饰的ViewSet 方法都将获得一个URL。

于 2013-07-03T16:24:06.237 回答
1

我将 def get_queryset(self): issue_id = self.kwargs['issue_id'] return Page.objects.filter(issue_id = issue_id) 添加到 PageList,现在 GET 适用于 issue/ /pages。现在我只需要弄清楚如何发布。

我将 def pre_save(self, obj): obj.issue_id = self.kwargs['issue_id'] 添加到 PageList 中,现在 POST 也可以了。从不存在的问题中查询页面会返回空结果,而不是 404 not found。如果有人知道更好的方法来做到这一点,我很想听听。

如果您的方法 get_queryset(self) 返回一个空列表而不是 404 NOT FOUND,我建议使用 django 的快捷函数 get_list_or_404。该方法可能如下所示:

from django.shortcuts import get_list_or_404

def get_queryset(self):
    filter = {}
    filter['issue_id'] = self.kwargs['issue_id']
    return get_list_or_404(self.queryset, **filter)

我知道这是一篇旧帖子,但也许这可以帮助其他遇到相同或类似问题的人。

于 2014-07-17T11:15:42.340 回答