我在 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
?