0leg
0leg

Reputation: 14144

Django REST: weird dot in url-reverse

The router code:

...
domain_nested_routers_lookup = 'domain'
router = routers.DefaultRouter()
router.register('accounts', DomainViewSet)
...
domains_router = NestedSimpleRouter(router, r'accounts', lookup=domain_nested_routers_lookup)

The reason for such a move is because project old API used the term domain, that is not getting changed to account.

The serializercode:

class DomainSerializer(...):
    link = serializers.HyperlinkedIdentityField(view_name='domain-detail', lookup_field='short_name')

Now the problem is that this code:

self.api_reverse('domain-detail', self.domain.id)

returns:

u'/rest/accounts/domain_0.1'

And I don't understand by what magic .1 is added (1 is an id of domain object).

The correct output should be:

u'/rest/accounts/domain_0

Upvotes: 0

Views: 452

Answers (2)

0leg
0leg

Reputation: 14144

After looking for an answer and help from 2ps, I have figured out the way:

self.api_reverse('domain-detail'

In my case, I had to look better how api_reverse is defined.

Upvotes: 0

2ps
2ps

Reputation: 15926

So you can pass in a set of parameters to reverse if there are parameters defined in your URL route. I haven’t worked with django REST before specifically, but I would try naming your parameter:

self.api_reverse('domain-detail', args=[ self.domain.id ])

OR -

self.api_reverse('domain-detail', kwargs={'pk': self.domain.id })

Which will probably generate a route like:

/rest/accounts/domain_0/1

Upvotes: 2

Related Questions