将django-oscarapi API ROOT扩展到自定义API类 [英] Extending django-oscarapi API ROOT to custom API class

查看:37
本文介绍了将django-oscarapi API ROOT扩展到自定义API类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a django oscar application and I use django-oscarapi for my custom APIs. Some things are missing from the oscarapi like category and promotions but I have been able to use django-restframework to create the category API but the challenge I am facing now is how to add it to the API-ROOT. This is my code for rendering categories

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'),

预先感谢

推荐答案

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

You have to override the root view of oscarapi. There maybe a way to partially override the module, but I was unsuccessful at this.

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

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

In your project add a file yourApp/api/views/root.py and paste in the contents of the source file above.

然后您可以通过向PUBLIC_APIS或ADMIN_APIS函数中添加一个元组来添加端点.

You can then add end points by adding a tuple to the PUBLIC_APIS or ADMIN_APIS function.

这篇关于将django-oscarapi API ROOT扩展到自定义API类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆