Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoReverseMatch for api_dispatch_detail and api_dispatch_list in Django 2.2 #1613

Open
dharvey-consbio opened this issue Aug 6, 2020 · 0 comments

Comments

@dharvey-consbio
Copy link

Given a project that defines a set of Tastypie endpoints:

project.api

from tastypie.resources import ModelResource

class ProjectResource(ModelResource):
    ...

project.urls

from django.conf.urls import include, url
from tastypie.api import Api
from .api import ProjectResource

api = Api(api_name='v1')
api.register(ProjectResource())

app_name = 'project'

urlpatterns = [
    url(r'^api/', include(api.urls)),
]

And another project that depends on Django 2.2 and that project, which includes the urls defined by it:

another_project.urls

from django.conf.urls import include, url

app_name = 'another_project'

urlpatterns = [
    url(r'^', include('project.urls')),
]

A call to ProjectResource.get_resource_uri with either api_dispatch_list or api_dispatch_detail returns an empty string, which causes the 'resource_uri' returned with objects on that resource to always be empty strings.

The reason is because in the context of another_project, Django is expecting project:api_dispatch_list and project:api_dispatch_detail. The app_name defined in project.urls needs to be prepended to the view name. So in queries to ProjectResource through another_project, the get_resource_uri method encounters NoReverseMatch and populates 'resource_uri' with an empty string for all objects returned.

In my case, because I manage both projects, my workaround was to override project.api.ProjectResource.get_resource_uri as follows (notice "project:" in the method signature and the first if statement):

project.api

from tastypie.resources import ModelResource

class ProjectResource(ModelResource):
    def get_resource_uri(self, bundle_or_obj=None, url_name='project:api_dispatch_list'):
        if bundle_or_obj is not None:
            url_name = 'project:api_dispatch_detail'
        try:
            return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))
        except NoReverseMatch:
            return ''

Can Tastypie be made to better support use cases like the above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant