Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using django models across apps?

So in my Django project I have a few different apps, each with their own Models, Views, Templates, etc. What is a good way (the "Django" way) to have these Apps communicate?

A specific example would be a Meetings App which has a model for Meetings, and I have a Home App in which I want to display top 5 Meetings on the home page.

Should the Home App's View just query the Meetings App's Model?

It feels like that is crossing some line and there might be a more de-coupled way to do things like this in Django.

like image 320
rmontgomery429 Avatar asked Jan 13 '10 04:01

rmontgomery429


People also ask

Can Django apps share models?

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.

How many apps can a Django project have?

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

What is the difference between project and app 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

At some point your apps will have to couple in order to get any work done. You can't get around that.

like image 72
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 22:11

Ignacio Vazquez-Abrams


To achieve decoupling as much as possible,

You need to have a Project specific app, that does all the hooking up things between each other.

Using signals from models to create new models in a decoupled apps helps. But doing too much of this, is foolish.

like image 29
lprsd Avatar answered Nov 15 '22 22:11

lprsd