multiple model files or multiple apps? - django

hello all i am building a project in django, and i am in doubt for some things and it would be great if you guys give me a good explanation about this issue.
i am planning to build a project which will work with DRF(Django Rest Framework) API. And the project is big so is it better:
to separate models into subfolders:
models.py
/submodels
models1.py
models2.py
or to create multiple apps like :
/Users
/Campaigns
/Billing
/Statistics

I personnaly would have make multiple apps if the API you are making will be consume by multiple applications.
Otherwise you can just make different models, so your project will be easier to manage and deploy.

I would suggest also the multiple apps approach (not doing submodels), i recently had experience with Django Rest Framework and its far easier keep everything separated by apps and define the views accordingly for the nested URLs.

Related

How do you add Vue to Django?

I'm am trying to add a reactive Vue js on the frontend and have Django serve the different pages on the backend. But how do you add these guys together?? I have found different ways to do so, but I can't find a good explanation on the why they are tying them together that way. I was hoping for a comprehensive explanation as to how these 2 are added together but also the why? I am still new to both of these frameworks so I am trying to understand not only how to do something but the reason for my doing them. Thanks in advance.
There are many guides on internet about how to use Vue and Django together. I provide you the main differences and a sample link of implementation for each one.
There are two main options for that:
Using Vue in Django templates
Separating Vue and Django projects
Option 1 is usually used when you want to use vue in some parts of the app or just making some reactive components. Your routes are handled by django and your passing data to frontend by passing data to views. In some cases you're a backend developer and don't want to spend time on frontend so you use this option whenever you need. For implementation check out this link.
Option 2 is when project is large and complex or you prefer to make a SPA (check this link) so you separate frontend and backend projects and interactions between these two is via API. So you run your frontend project separately and routing and other stuff is handled by vue. For implemetaion check out this link.

What should be the directory structure for a REST API in Django?

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?
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.
Django Models -> MVC Models
Django Views -> MVC Controllers
Django Templates -> MVC Views
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:
fat models: you put all your business logic inside the models
services: you have separate modules containing methods/classes for your business logic
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.

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.

What is a Django "app" supposed to mean?

I'm new to Django and trying to understand the preferred means of operation when deploying web applications.
Let's say I'm making a web application with (for example) user login management, some uploading functionality, manipulation of uploaded files, and rendering uploaded files on screen. They're all part of the same "web application".
Would each of these functions be its own app in the project, or should these all be together a single app? Is a Django app intended to correspond to a web application, or does it correspond to a single set of functions interfacing with a few tables in the database?
There's a distinction to be made between reusable apps and non-reusable apps. For reusable apps it's essential that they offer well defined functionality and are intended to solve a well defined problem. If this wasn't the case, they wouldn't be very reusable.
However you're likely to also have some non-reusable apps, i.e. one or more apps in a project that implement application logic that's specific to the project. In my projects I always have a non-reusable app called core that acts as glue and ties everything together. If I have distinct sections in my site I may choose to have more non-reusable apps, because I like the way it essentially namespaces my project (e.g. models, views, templates, etc.)
A Django app is a group of related functionality used to complete or maintain one aspect of a site. The web application you describe would be separated into at least 2 Django apps, depending on how granular you want to make the handling of the uploaded files.

Content scrapers and Django

I have an application built with Django. Part of it relies on data that I aggregate from other websites. Wondering how I should approach building the scraper/aggregator.
The advantages I see of building it as a Django app is
the ability to use Django's models & database API
the ability to use Django's other methods
On the other hand I think the disadvantage would be scalability in the long run.
Should I build the scraper/aggregator as an app in my Django project or as a separate script that runs on its own?
Would love to hear your thoughts.
Neither of your points require it to run within Django. And since it will not be dependent on the web/HTTP interface, having it be a separate module is the only option that makes sense.
I just have published a Django app django-dynamic-scraper on GitHub, which is build on top of the scraping framework Scrapy and where you can build Scrapy scrapers in the Django admin and use Django model classes to store your scraped data, maybe this is of some use for people with similar problems.
If it's a django app, it will only run when someone loads the page. That could slow the loading.
Making another script could be a nicer idea but could produce inaccurate data.
I think it actually depends on the context.