django - 将 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'),
提前致谢
解决方案
您必须覆盖oscarapi
. 也许有一种方法可以部分覆盖模块,但我没有成功。
要覆盖的模块: https ://github.com/django-oscar/django-oscar-api/blob/master/oscarapi/views/root.py
在您的项目中添加一个文件yourApp/api/views/root.py
并粘贴上面源文件的内容。
然后,您可以通过将元组添加到 PUBLIC_APIS 或 ADMIN_APIS 函数来添加端点。
推荐阅读
- php - 使用阿拉伯语导入 Calibri 字体不起作用
- solidity - 如何在 Solidity 中编写国际象棋游戏?
- python - 如何阻止 Fiddler 访问我的应用程序 API 或在 Python 的后台进程中运行
- java - 如何为每个附加标签运行一次 TestNG 场景?
- java - 如何在 Hikari CP DEBUG 日志中隐藏连接密码
- ruby - 运行“bundle exec rspec”时出现问题
- json - 获取 API Django
- frontend - 仅用于前端的 CMS
- python - 如何使用 .instances() 方法遍历 owl:Thing 中的所有 owlready2 实例?
- css - 如何减小 CSS 渐变图案的大小