开发者

How should I use django models from other applications?

What is the proper way to use models from other django applications in my application? For example, should I simply go into the other application's models and add my functionality effectively coupling the two applications?

Should I subclass the models using a django proxy model to add my application specific functionality? This method, however, rules out ever using this added functionality in the parent application.

Should I simply import the model and create library functions to handle this ad开发者_开发知识库ditional functionality?

Thanks! Pete


There are certain categories of apps that end up being coupled in other apps - a good example is the django.contrib.auth: if you are using the Django User model you depend on that app and your own apps are coupled with it.

There is nothing wrong with that scenario. I tend to think of it less as coupling and more as dependency. Your app depends on the django.contrib.auth being available. It is a natural thing because you need that functionality. You can always replace that app with some other app that provides the same interfaces you are currently using...

Now, if you have two of your own applications and they need knowledge of each other's models, I can think of two scenarios:

  1. You need a third application for your shared models

  2. They should be only one application (uncommon; you had a reason to make them separate to begin with!)

I usually have a core application of my own (just like there is a django.core) where I keep the code that is common across the apps in my project. If models of different applications are dependent on each other, they get moved to this app.

Now, it is also possible that your two applications really are only one application - but that should not be the default thinking. Monolithic apps are a very bad thing (TM).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜