Sharing models between Django apps
Asked Answered
M

5

45

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

Mennonite answered 9/11, 2010 at 18:46 Comment(1)
note that in django model name should be singular.Reunite
R
30

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.

Rexfourd answered 9/11, 2010 at 19:4 Comment(2)
It's not just the same name, it's the exact same model class.Lasonyalasorella
The User and User statistics apps where just for the sake of example, because my case is not that easy to understand. This answered my question, thank you!Mennonite
E
43

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.

Eminent answered 9/11, 2010 at 20:47 Comment(0)
R
30

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.

Rexfourd answered 9/11, 2010 at 19:4 Comment(2)
It's not just the same name, it's the exact same model class.Lasonyalasorella
The User and User statistics apps where just for the sake of example, because my case is not that easy to understand. This answered my question, thank you!Mennonite
D
2

You cand try better extending the Django User model with inheritance. You will use the django user with custom field added, so you will have the same user for all the apps.

Dulcia answered 9/11, 2010 at 20:46 Comment(1)
I've seen several people on IRC (freenode#django) in the past who have run into problem after problem when trying to subclass auth.User. In general, it's one of those things you should stay away from unless you know you need it. As an aside, user profiles are the generally preferred method of storing additional information about Users.Rexfourd
L
1

You can directly import models in app2/models.py. Usually you might need a foreign key, which looks like

models.ForeignKey('app1.ModelClass1', on_delete=models.CASCADE, related_name='modelclass2')
Lucky answered 28/8, 2021 at 3:31 Comment(0)
L
-5

Don't do this. They will have the same app name and the ORM will be confused. Use an abstract model instead, and have both derive from it.

Lasonyalasorella answered 9/11, 2010 at 18:58 Comment(3)
Haven't you misunderstood what the OP is asking? He doesn't say anything about defining models identically in different apps, he's talking about importing the User class into his app so he can get data from it, surely? IOW, the answer to this question is "Yes, of course you can."Diez
@Daniel: It is possible that such is the case. If the OP clarifies what he is looking for then I will revise.Lasonyalasorella
You should consider revising it since you were clearly wrong about your interpretationMorse

© 2022 - 2024 — McMap. All rights reserved.