How reverse an include pattern path in Django

225 Views Asked by At

Hi i am wondering how i can reverse a urlpattern path that uses include pattern. Since there is no namespace i don't know how to reverse it. Also i am new to Django so all this is a little bizarre to me.

v1_api = Api(api_name='v1')
urlpatterns = [path('', include(v1_api.urls))] + restless.get_urls()

I want to use reverse('something') from django.urls import reverse

  class SiteResource(ModelResource):      
    baselayer = fields.ToOneField(
    'DataLayerResource',        
    'baselayer',
     null=True)

 class Meta(ModelResource.Meta):        
      queryset = models.Site.objects.all()        
      resource_name = 'site'        
      authorization = SiteAuthorization()       
      validation = CleanedDataFormValidation(form_class=SiteForm)        
      detail_allowed_methods = ['get', 'post', 'put', ]        
      list_allowed_methods = ['get', 'post']        
      excludes = []        
      abstract = False        
      filtering = {            
      'site_name': ALL,            
      'verbose_site_name': ALL,            
      'site_id': ALL,            
      'site_path': ALL,            
      'extent': ALL,        
}
1

There are 1 best solutions below

0
Saiprasad Balasubramanian On

As per Tastypie's source code there are a few standard methods which are implemented which you can find over here

The basic pattern to reverse a URL in Tastypie is

a = reverse('<method>', kwargs={'resource_name': '<name of the resource>', 'api_name': '<api_name>'})

In your case, let's assume you want the URL for api_dispatch_list. Your name of the resource is site and api_name is v1

So the URL in this case would be

url = reverse('api_dispatch_list', kwargs={'resource_name': 'site', 'api_name': 'v1'})

and would come out to /api/v1/site/