修改 oscarapi 以将某些端点显示为来自管理端点的 public

Modifying oscarapi to show some endpoints as public from admin endpoints

我想要实现的是自定义 oscarapi 以将合作伙伴 api 公开为 public api 而不仅仅是管理员 [=16] =]

我遵循了有关如何自定义 api 的文档,并且还按照 Jamie Marshall 在 Extending django-oscarapi API ROOT to custom API class

到目前为止,我可以覆盖 root.py 文件,但无法获取 oscar 查看新的 urls.py 文件。

目前我的工作如下

  1. 我创建了一个 api_customization/views/partner.py 文件
  2. 我创建了一个 api_customization/views/root.py 文件
  3. 我试图通过创建 api_customization/urls.py 文件来扩展 urls.py 文件

但是,我收到以下错误

  Traceback (most recent call last):
    File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request)
    File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/usr/local/lib/python3.7/contextlib.py", line 74, in inner return func(*args, **kwds)
    File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch response = self.handle_exception(exc)
    File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception self.raise_uncaught_exception(exc)
    File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception raise exc
    File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 506, in dispatch response = handler(request, *args, **kwargs)
    File "/usr/local/lib/python3.7/site-packages/rest_framework/decorators.py", line 50, in handler return func(*args, **kwargs)
    File "/code/.../forked_apps/api_customization/views/root.py", line 52, in api_root apis = PUBLIC_APIS(request, format)
    File "/code/.../forked_apps/api_customization/views/root.py", line 29, in PUBLIC_APIS ("partners", reverse("partner-list", request=r, format=f)),
    File "/usr/local/lib/python3.7/site-packages/rest_framework/reverse.py", line 47, in reverse url = _reverse(viewname, args, kwargs, request, format, **extra)
    File "/usr/local/lib/python3.7/site-packages/rest_framework/reverse.py", line 60, in _reverse url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
    File "/usr/local/lib/python3.7/site-packages/django/urls/base.py", line 87, in reverse return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
    File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix raise NoReverseMatch(msg)
  django.urls.exceptions.NoReverseMatch: Reverse for 'partner-list' not found. 'partner-list' is not a valid view function or pattern name.
  [09/Jul/2021 21:18:13] "GET /api/ HTTP/1.1" 500 136185

views/partner.py

from oscarapi.utils.loading import get_api_class
from oscar.core.loading import get_model
from rest_framework import generics

PartnerSerializer = get_api_class("serializers.product", "PartnerSerializer")
Partner = get_model("partner", "Partner")

class PublicPartnerList(generics.ListCreateAPIView):
    queryset = Partner.objects.all()
    serializer_class = PartnerSerializer

urls.py

from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from oscarapi.utils.loading import get_api_class
from oscarapi import urls

PublicPartnerList = get_api_class("views.partner", "PublicPartnerList")

urls.urlpatterns += [
    path("partners1/", PublicPartnerList.as_view(), name="partner-list"),
]

urls.urlpatterns += format_suffix_patterns(urls.urlpatterns)

views/root.py

import collections

from django.conf import settings

from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse


__all__ = ("api_root",)


def PUBLIC_APIS(r, f):
    return [
        # other urls .......
        ("partners", reverse("partner-list", request=r, format=f)),
    ]

# remaining file content ......

我需要有关如何实现此目标的指导或提示 感谢任何帮助

我找到了解决方案,可能对某人有帮助

我必须对 root.py 文件执行相同的步骤才能让我的应用程序识别自定义 urls.py 文件

所以我做了什么

  1. 复制urls.py
  2. 的内容
  3. 修改文件以满足我的需要
  4. 更新我的应用 urls.py 文件以指向 api
  5. 的自定义 urls.py 文件
    //So from this
    path("api/", include("oscarapi.urls")),
    //To this
    path("api/", include("APP_NAME.forked_apps.api_customization.urls")),