Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing models between Django apps

I will be brief: to work in the spirit and idea of a Django app, it is ok for an app to import models from inside another app ? Say, a User statistics app will import models from a User app something like: from users.models import users

like image 418
Nicu Surdu Avatar asked Nov 09 '10 18:11

Nicu Surdu


People also ask

How many apps can a Django project have?

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

What is the difference between APP and project in Django?

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.


2 Answers

The answer is yes. It's perfectly okay for one application inside your django project to import models from another application. The power of a django project lies in the apps and their interactions.

Also make sure that you have utility applications importing models from more generic applications and not the other way. So "userstatistics" app should import models from the "users" app but "users" app should not rely on the "userstatistics".

If your app is importing models from a 3rd party application (lets say django-piston), be sure to specify that in a requirements file.

like image 176
tarequeh Avatar answered Sep 20 '22 12:09

tarequeh


If you're building an internal app that has no chance of ever being released to the public, sure, do whatever you want.

If you're building an internal app that has little chance of ever being released to the public, but will possibly be used by future/current developers, sure, but be sure to document what the app needs to work properly.

If you're building an app for public release, try to keep it self-dependent (and django-internals dependent, ie, use what django provides, when possible). If you really need a third-party app to work, or if a third party app would make your code more manageable, then sure, include dependencies, but be doubly sure to document all requirements and necessary setup.

In most cases, you can do almost whatever you want so long as you have sufficient documentation.

I do, however, have to question the sanity of making your own User model that has the same name as django's builtin auth.User.

like image 26
eternicode Avatar answered Sep 21 '22 12:09

eternicode