Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django REST framework, get object from URL

I'm wondering if there is a clean way to retrieve an object from its URL with django rest framework. Surely there should be, as it seems to be what's happening when using HyperlinkedRelatedField.

For instance, I have this URL /api/comment/26 as a string. From my view, how can I get the comment instance with pk=26?

Of course I could redo the work and work on the string but it must be a better way?

Thanks a lot.

EDIT:

This is how I solved it at the end:

resolve('/api/comment/26/').func.cls.model will return my model Comment. resolve('/api/category/1/').kwargs['pk'] will return the pk.

Which gives you:

from django.core.urlresolvers import resolve

resolved_func, unused_args, resolved_kwargs = resolve('/api/category/1/')
resolved_func.cls.model.objects.get(pk=resolved_kwargs['pk'])
like image 464
Vincent Alvo Avatar asked Jan 28 '14 13:01

Vincent Alvo


1 Answers

I suspect your best bet would be to manually keep a map of models to url patterns — not too dissimilar from a URLConf.

If that doesn't rock your boat you could pass the path into resolve.

This will give you a ResolverMatch upon which you'll find the func that was returned by the as_view call when you set up your URLs.

The __name__ attribute of your view function will be that of your original view class. Do something like globals()[class_name] to get the class itself.

From there access the model attribute.

I hope that helps. As I say, you might just want to map models to URLs yourself.

like image 170
Carlton Gibson Avatar answered Oct 22 '22 11:10

Carlton Gibson