6

I am fairly new to Django and have some requirements that involve creating a Django application to be the front end for an API server.

This app should have little need to ever store data in a local database. Instead, we are to access a REST API to get and post data.

Being less familiar with the framework, where should I be placing the logic for getting and manipulating data on the remote API? My initial thought was to place this into the models.py file, but models seem to be designed specifically for database access in Django.

So then I placed request calls in my form classes's get/post functions. However, this seems like I am mixing up the data logic with the views. Also, I noticed that reloading pages after posting a form would result in the form containing the original GET request for the form until I restarted the server.

It appears that I should be placing this logic elsewhere, but I'd like someone to explain what is "standard" in this context.

I am sure I not the first person to encounter this and would like some direction on how other projects handle this in Django. Thank you for your guidance.

2 Answers 2

2

I'm working on a similar problem when I'm trying to wrap the Django ORM around an API, and I'm surprised that no one else has done something like this before. (Or at least they haven't made a public gist or blogpost about it.)

I think your intuition is right that the logic probably shouldn't go in the view or forms. Ideally it would be lower-level (Model, Manager, Queryset or lower), so that you get all the benefits of the rest of Django.

I think the most ideal way to do this would be to create a custom database backend that simulates the functionality of a database, while interacting directly with the API.

Here is a high-level overview of what that entails: http://reinout.vanrees.org/weblog/2016/11/04/database-backends.html

Also, if you look at the Django sourcecode, you could probably use django/db/backends/dummy/ as a starting point for how the code should be structured.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure if Django is the right framework for purely handling Ajax GETs and POSTs and such.

If I understood you correctly, you will basically just utilize the front end of Django. One of Django's strength is its ORM with databases in the background.

Wouldn't a Javascript frontend-only app be better, faster and even slicker? Like a Vue.js based one

1 Comment

Thanks for the suggestion, but Javascript-only will not work due to cross origin limitations. Also, Django is being used to build out a few apps on the project like an admin system and some other requirements. But this particular part in our part is just making a bunch of API calls to another server to manipulate it. So while there might be some merit to using some other framwork or language, in my circumstance, I am more than likely "stuck" with Django in this case. The continuity of the project is probably more important long term than picking the best tool for one task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.