Reputation: 3481
I am starting with developing REST API's with the Djano framework. Previously I have worked extensively with Java related frameworks specifically with SpringBoot.
If I take an example of a REST API in Java based framework below is the directory structure that I have mostly used :
So something mostly on the lines of a MVC Design Pattern. Also in java mostly each class is written on a new file, however the tutorials that I have been looking around writes multiple classes within one file, is this the best practice around Django?
Also I see that in Django we create a project and then inside it we create an app. Are these app equivalent of package in java?
Also, in SpringBoot and other java related frameworks we normally follow a Top-Down approach, where a Controller recieves a call which is further passed on to the Service where all business logic resides which then further calls DAO(Repository) for CRUD and the corresponding result is passed from DAO to Service back to Controller which generates the formatted response.
What I have moslty seen in Django is all the logic is written on views which calls Model for CRUD. Don't we follow SOLID priciples here?
If I take an example of a library management system, How will one design that around the Django framework?
Upvotes: 3
Views: 3144
Reputation: 29967
This is a great question, but I'm not sure that StackOverflow is the right place to ask it, as there is no clear-cut answer. That said, I'm trying to give it a shot.
Django also follows the MVC design pattern, but the naming is a bit different.
It is often referred to as MTV instead of MVC. But that's not the end of the story, because many people think it is a bad idea to put too much business logic in the views, for reasons like testability and reusability. There are two common approaches to this issue:
Personally, I have worked with both and had better experiences with the services approach.
The equivalent to a DAO in Django is probably the Django ORM. Note that this is really only an abstraction layer for the supported relational databases. If you want to use another datasource like a NoSQL DB or a REST API, you have to roll your own solution or look for third-party packages.
Java can only have one public class per file and they must have the same name. Python does not have this limitation, so it is indeed a best-practice to have multiple classes per file (.py files are called modules). If a module/file becomes too large, you can turn it into a package (a directory with a __init__.py
file) containing several modules (and/or sub-packages).
The differentation between app and project can be confusing, so I have written a whole article about Django's naming scheme. A Django app is a Python package, but they are usually organized by domain. E.g. in a library management system, your library
project might have an app catalogue
to manage the inventory, and a checkout
app to manage the process of checking out books.
Finally, if you are specifically aiming to build a REST API, I highly recommend you have a look at Django REST Framework.
Upvotes: 6