Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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

Yep, that's how it's done. If you really want to decouple things, you could make your Home app use generic foreign keys, and some sort of generic template system, but there's not really a good reason to, unless you have grand plans for your home app being pluggable and working with a bunch of other different Django apps.

Writing tightly coupled Django apps is really easy, and writing decoupled Django apps is really hard. Don't decouple unless you have a reason to, and you'll save yourself a lot of work (and happiness!).

If it were me, I would make a template tag in your meeting app that produces the desired output and include that template tag in the home app's template.

That way you are only coupling them in the View portion of the MVC and makes it easier to maintain if you change your models in the meeting app.

For your specific example, I would use a Django templatetag.

Having a templatetag "display_top_meetings" in your Meetings app, and calling it with {{ display_top_meetings 5 }} from your index template, loading it first.

You can read more about templatetags here:

Django Official documentation about TemplateTags

B-List's article on writting 'better template tags'

I hope this help!

yes. I think thats a design feature. All models share a backend, so you'd have to do extra work to have two models with the same name in different apps.

Projects should not share Models

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top