Using django models across apps?
Asked Answered
F

6

19

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.

Fogdog answered 13/1, 2010 at 4:15 Comment(1)
possible duplicate of Sharing models between Django appsLikewise
A
18

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

Auteur answered 13/1, 2010 at 4:16 Comment(0)
B
3

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.

Buskirk answered 13/1, 2010 at 7:37 Comment(0)
B
1

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!).

Boeotia answered 13/1, 2010 at 4:18 Comment(1)
"Writing tightly coupled Django apps is really easy, and writing decoupled Django apps is really hard" -- I hear what you are saying here... "Don't decouple unless you have a reason to, and you'll save yourself a lot of work" - Doesn't this go against the sound software engineering principles of keeping systems as loosely coupled (in the first place) as possible?Baroness
U
1

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.

Unconcerned answered 13/1, 2010 at 4:23 Comment(0)
D
1

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!

Discomfortable answered 25/1, 2012 at 22:7 Comment(0)
R
0

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

Rehearsal answered 13/1, 2010 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.