does django-rest-framework allow interal / local calls without an http request?

1,665 views
Skip to first unread message

Steve Sobel

unread,
Jul 6, 2015, 4:13:16 PM7/6/15
to django-res...@googlegroups.com
My apologies if my choice of google search terms was poor and this is clearly documented somewhere. I have searched high and low and been unable to find a good answer on this:

As I find myself building new views, I see a problem of duplication of code / logic:

From a rendering efficiency standpoint on the web, it's better to render templates on the server side rather than render the page and rely on the browser to make an HTTP request to our API for that page.  That's slower (you must wait until document.ready to even start) and adds more HTTP requests (not good for people on mobile networks, especially).

So what I end up with is a traditional django view / controller that's basically doing the same work that an API endpoint call would be doing.  If I add a new sort/filter method to a list view, then, I need to sync up the changes I make in both the API and the django controller (say a new URL param is accepted or what have you).

It would be great if I could call a view internally, and instead of returning serialized data (e.g. JSON, etc) - I just got the same queryset.  This way, I wouldn't be updating logic in two places with the possibility of forgetting / letting them get out of sync. Does this exist?  I do see the docs for TemplateHTMLRenderer - that is close to what I want - but I want the result of a call there without making an HTTP request.

I did find this slight allusion to a similar request but it's quite old and seems to be a dead end: 


I would appreciate any input / insight, thanks!

Steve

Filipe Ximenes

unread,
Jul 7, 2015, 9:02:45 AM7/7/15
to django-res...@googlegroups.com
On Mon, Jul 6, 2015 at 5:13 PM, Steve Sobel <st...@openingbands.com> wrote:
My apologies if my choice of google search terms was poor and this is clearly documented somewhere. I have searched high and low and been unable to find a good answer on this:

As I find myself building new views, I see a problem of duplication of code / logic:

From a rendering efficiency standpoint on the web, it's better to render templates on the server side rather than render the page and rely on the browser to make an HTTP request to our API for that page.  That's slower (you must wait until document.ready to even start) and adds more HTTP requests (not good for people on mobile networks, especially).
Well, it depends... from what i could understand, you are trying to make a normal HTTP request with a base template and then fetching "API data" every time the user requests a page. This is not how APIs are normally consumed. A more efficient way would be to fetch the "base template" only one time and then only swap data in the template or small blocks of the page. This is know as SPA (single page application) and is how frameworks like Angular.js works. It gives the user a much more fluid interaction with your application since the page "never reloads" and it's much faster to fetch only the page data than to fetch data and layout.

So what I end up with is a traditional django view / controller that's basically doing the same work that an API endpoint call would be doing.  If I add a new sort/filter method to a list view, then, I need to sync up the changes I make in both the API and the django controller (say a new URL param is accepted or what have you).
Sounds like you are trying to buid an isomorphic application =)

It would be great if I could call a view internally, and instead of returning serialized data (e.g. JSON, etc) - I just got the same queryset.  This way, I wouldn't be updating logic in two places with the possibility of forgetting / letting them get out of sync. Does this exist?  I do see the docs for TemplateHTMLRenderer - that is close to what I want - but I want the result of a call there without making an HTTP request.
One approach to make this easier would be to have your querysets in a separate function that can be called both from your django view and also from your API view. 
Hopefully you are using class based views, if so, you can simply write a mixin that defines the "get_queryset" and use it in both, django and DRF.

I did find this slight allusion to a similar request but it's quite old and seems to be a dead end: 


I would appreciate any input / insight, thanks!
I apologize if I misunderstood something, if so, please send some code examples so I can take a closer look at your situation and try to help.
 

Steve

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

Steve Sobel

unread,
Jul 7, 2015, 1:40:40 PM7/7/15
to django-res...@googlegroups.com
Well, it depends... from what i could understand, you are trying to make a normal HTTP request with a base template and then fetching "API data" every time the user requests a page. This is not how APIs are normally consumed. A more efficient way would be to fetch the "base template" only one time and then only swap data in the template or small blocks of the page

I think I may not have adequately explained what I want to do. That's my fault.

Let's say for the sake of argument I'm doing something really simple like writing a blog app.  I want that app to have an API so people can pull JSON feeds of the blog posts too.

so if you go to www.example.com/posts - it would be a listing of all blog posts.  I'd have a normal django route in urls.py, and it'd be a ListView. I'd use django templates to render it.

Now I add an API endpoint that also gives out those posts in JSON form. It's /api/posts or some such.

These end up being two "separate" bits of code, right? I've got my django listview, and I've got my django-rest-framework listview.

What I'm wondering is if there's a way to use the same code for both - so that when I add a filter or sort field, for example, I don't need to update both views with that field. 

On a simple example like a blog this is obviously over-optimization - but if you imagine a huge webapp with tons and tons of models/controllers I hope it's clear why one might want to do this.  Does that make more sense?

Steve


--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/26tIiJB7vQw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.

Filipe Ximenes

unread,
Jul 7, 2015, 3:08:54 PM7/7/15
to django-res...@googlegroups.com
Oh, ok. So you should think about the mixin idea. Use class based views for everything and add a mixing that overrides the "get_queryset" method.
Reply all
Reply to author
Forward
0 new messages