首页 > 解决方案 > 将 django-oscarapi API ROOT 扩展为自定义 API 类

问题描述

我有一个 django oscar 应用程序,我将 django-oscarapi 用于我的自定义 API。oscarapi 中缺少一些东西,比如类别和促销,但我已经能够使用 django-restframework 创建类别 API,但我现在面临的挑战是如何将其添加到 API-ROOT。这是我渲染类别的代码

customapi 序列化程序类

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ('id', 'numchild', 'name', 'description', 'image', 'slug')

意见

class CategoryList(generics.ListAPIView):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer


class CategoryDetail(generics.RetrieveAPIView):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer

customapi/urls.py

url(r'^caty/$', CategoryList.as_view(), name='category-list'),
url(r'^caty/(?P<category_slug>[\w-]+(/[\w-]+)*)_(?P<pk>\d+)/$',
        CategoryDetail.as_view(), name='category'),

提前致谢

标签: djangodjango-rest-frameworkdjango-oscar

解决方案


您必须覆盖oscarapi. 也许有一种方法可以部分覆盖模块,但我没有成功。

要覆盖的模块: https ://github.com/django-oscar/django-oscar-api/blob/master/oscarapi/views/root.py

在您的项目中添加一个文件yourApp/api/views/root.py并粘贴上面源文件的内容。

然后,您可以通过将元组添加到 PUBLIC_APIS 或 ADMIN_APIS 函数来添加端点。


推荐阅读