Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle common code in a django project which is used by multiple apps

Tags:

python

django

diving deeper into django I came across the challenge to handle code which is not specific to 1 app but is shared/used by multiple apps.

I would not(!) like to store it as part of an app (to avoid app dependencies) but to have it in a specific place.

currently my best practice is to create a django app "shared" in which I place this code/functionality

so my project structure looks similar to this:

mysite/
    manage.py
    mysite/
     ...
    shared
     ...
    app1
     ...
    app2
     ...
    app3
     ...
    ...

is there a "django best parctice" or a more practical way how to handle this?

like image 664
udo Avatar asked Sep 13 '15 13:09

udo


People also ask

Can a Django project have multiple apps?

Django's features ensure you can link multiple related project apps, create apps that accept and store user information, and trigger actions, such as sending emails.

How many apps can a Django project have?

Django comes with six built-in apps that we can examine.

In what context project is different from app in Django framework?

A project refers to the entire application and all its parts. An app refers to a submodule of the project. It's self-sufficient and not intertwined with the other apps in the project such that, in theory, you could pick it up and plop it down into another project without any modification.

Does order of installed apps matter Django?

The order of INSTALLED_APPS is significant! Django applies the following algorithm for discovering translations: The directories listed in LOCALE_PATHS have the highest precedence, with the ones appearing first having higher precedence than the ones appearing later.


1 Answers

I usually do exact same thing what you are doing. Not sure if that is best practice however I've seen other people using the same approach. I like it because:

  • when the shared/core/etc app becomes useful in other projects, you can package it as reusable app which can be installed via pip in other projects
  • it does not interfere with existing apps in the project

The only note about packaging it as a reusable lib is that I would recommend to rename it to something other then shared. The reasons is that when you package it in PyPI lets say as my_django_utils, then you will have to change all your imports in all the projects. If you come up with a generic name now, then you can easily package it in the future without needing to change all your imports...

like image 115
miki725 Avatar answered Oct 11 '22 14:10

miki725