Project structure for API only Django project - django

I want to create a Django based backend providing an API using django-rest-framework only. The admin interface is the only visual interface I need for end users. The API shall be used by/integrated with a JS frontend. The backend consists of several parts like configuration, visualization, etc. According to design best practices I'd first create a Django project my_project with django-admin startproject my_project . and add an app per part with python manage.py startapp configuration, python manage.py startapp visualization, etc. (In the django-rest-framework quickstart there is generated one Django app.)
To me it's not clear how I have to adopt the Django design best practice of using apps to RESTful API based JS frontend integration. In case I want to integrate the backend with a JS frontend how should I structure my codebase? Should I create apps configuration, visualization, ... (I don't create template based views) and define corresponding models with a single RESTful API. Where should I place the API sources w.r.t. project structure? How should I map the API to models?

DRF extends Django views and sort of replaces forms with serializers. So you can use exactly the same structure with Django. Or if you like, you can move the DRF modules to a separate api package either for each app or project-wide. It all depends on you. But to keep it simple since it's an API only project, you can just use the normal Django app structure with DRF modules flat in the apps

Related

General questions about Django and DRF

I've made a project using Django and Bootstrap (I plan to use React for the frontend). Then I started building REST API with DRF and it seems that my DRF views are pretty similar (if not duplicative) to my original Django views such as DetailView, UpdateView and etc.Now I wonder if I can keep my old views or should I completely replace them with DRF ones? Or should I use DRF views for CRUD and keep particular Django views?
And another qusetion: am I right that when working with Django and DRF Django becomes just an ORM provider also responsible for business logic, authentication and some other features?
Thanks.
Django is a fullstack web development framework. Here you can progress in different ways. You can use Django and ReactJS without having to develop any RestAPI. It depends on how you approach it with ReactJs.
Do you want to develop the frontend entirely with ReactJS? If so, you can develop your backend directly using DRF and consume APIs on the frontend only with React.
While developing a full stack with Core Django, if you want to make the Frontend dynamic, you can use Django and DRF together, or you can develop APIs without using DRF.
You can use DRF and Core Django together for different scenarios, or you may only need one of them. It depends on you and your project.
Edit for second question:
Django Rest Framework requires Django. Also, with Django you can do everything DRF does. By writing more code of course. This process provides many conveniences with DRF; like authentication modules, json serializers..

Best way to structure a Django project

I'm new to django and plan on building a large django project.
I'm starting to implement cookicutter django after reading through "2 Scoops of Django" but still have some questions on structuring a project.
I've setup my project, we'll call it 'business_proj'. In business_proj I started an app called 'accounting' this might have an accounting dashboard for users in the accounting security group. Now, what if I want to have apps that belong to accounting, such as 'invoices' and 'purchase_orders'? Should I create those apps inside my accounting app? Or should I create all my apps in the main project root? The way I've started doing it is creating child apps inside of their parent apps but some parent apps are so big that even this gets messy. Is there a better way to do this? Thanks
If you have gone through Two Scoops of Django then you should probably check out Django Cookiecutter which has been created by the authors of this book.
Django Cookiecutter is an excellent boilerplate for starting a Django application both for personal use and for production.
They also have excellent documentation which will help you with the best practices for Python and Django coding.
Check out: Django Cookiecutter Git Link
I suppose it depends on the tastes of each developer. I split each funtionality in a separate app for example. I have never used child apps. Excepts when I work with Django Rest Framework. With Django Rest Framework I create a child app of each up for REST funtionality.

How to plug Angularjs / Restangular Project with Django / Tastypie?

I'm trying to shape a project with AngularJs and Restangular, as a web app, and Django / Tastypie as an API called by the app.
I have :
- an angular app doing well when run with the web-server script from the seed app.
- a django / Tastypie API returning json when urls are called.
But I don't know how to connect them ! How do I start the app with my Django API ? How should I load it ? I don't find many examples on the web ...
Many many thanks for your help.
There're two main integration libraries, Djangular and Django-Angular
Djangular provides various scenarios, but the one that makes most sense to me is this:
Create your Django app, and use Tastypie/DRF as you can to produce
an API.
Use Djangular to create a new app with manage.py startangularapp
<app_name>, which gives you the base layout for including angular.
Define a 'passthrough' url for each app, in your urls.py that points
to an index.html for each app, which as minimum contains <div
ng-view></div>.
In your app.js provide routes using the $routeProvider. You'll
need to inject DjangoProperties in to the dependencies, and use
DjangoProperties.STATIC_URL to get the right path.
Write a service that consumes the your API from 1.
Write the rest of your AngularJS app around this API
Share and Enjoy!

Client JS + Django Rest Framework

After reading this article about separating server and client, Separate REST JSON API server and client?
I want to know if this problem also exists in Django.
In Django, are these 2 separate phenomenons?
Django is MVC. Take out the view, have django as the backend and just send JSON. Create a separate say Emberjs or Angularjs app. Make these clients access backend's REST resources. In this case, how can you put the projects together to deploy to Heroku? Can you just deploy a "JAVASCRIPT-HEAVY-CLIENT" to Heroku, and have it just talk to ur REST server?
Since Django is MVC, don't completely take out the view, but integrate Emberjs/Angularjs into the View, but still use REST resources? In this way, you can put all the angularjs components/js files into the static folder and deploy to Heroku.
Are these the same? and realistically, how do you put them together to be able to deploy to Heroku?
Heroku is a application server platform. It's not really designed to serve static code.
The approach I've previously taken is to build the Django part normally, and have a single view that serves out a bootstrap template for everything under the JS App root.
Say I have a {Angular,Ember} app living at mydomain.com/app/, then everything under that will serve the bootstrap template (which includes serialised values queried from database) and calls the JS boostrap method to startup your app, and then the app takes over routing from that point, and renders out it's views.
At this point, all the data for the views is coming from django-rest-framework/django-tasypie.
Using this method and leveraging django-pipeline & django-boto's S3 storage backend, You should be able to serve a decently sized project with Django & {Angular,Ember}

Django app as REST-based service

According to the documentation, an app is a module which deals a well defined operation.
Is it correct to think about an app as a REST-based service? or is it mandatory to use some framework like piston or tastypie to create a RESTful web service around an app?
Generally, no. Django app is really just a python module, with some interfaces to django internals like models, urls, admin discovery etc.
To implement REST, you still have to manage network communications via views, and this is where you either write your own code or use the help of tastypie/piston/etc.
Please do have a look at django-rest-framework, I just stepped over from tastypie to this new framework, works great!
http://django-rest-framework.org/
Especially the class based views and the browsable api! and may other advantages (e..g. to upload images)
And to answer your question:
The rest-base service is an extra entry to your webapp.
I made some api's for some projects using the django-rest-framework, most project members were surprised they got a webapp as an extra, while it was actually the other way around. You make a django app (with views models and urls) and on top off that you make the api.