Django 和架构:如何分享“参考”项目之间的数据库?
今天我带着一个关于 Django 的设计/架构问题来。
我在多个网站(托管在同一服务器上)上工作,这些网站分别需要地理数据(州、城镇等)。每个项目都包含应用程序,并且每个应用程序可能包含带有城镇或州的 ForeignKey
字段的模型。
为了不重复自己,我不想建立一个数据库来存储这些城镇和州,并通过 Django 项目使用它。
Django 提供了一种在同一项目中使用多个数据库的简单方法,在 settings.py
文件中声明它并编写路由器类来保存读写内容。但这样一来,就不可能使用 select_lated
语句,例如:
job = get_object_or_404(Jobs.objects.select_related('state__town'), user=user)
这种行为对我来说很自然(不可能从头开始在数据库之间进行连接)...
我的问题:
- 考虑引入是一个好主意吗dblinks (我不这么认为...)Django 可以处理它吗(我没有找到这部分的任何文档)?
- 面对这种情况,你会如何进行?
一个快速而肮脏的解决方案是在每个项目数据库中导入所有地理数据(城镇、州......),但它根本不是干的:(:
python manage.py loaddata geo.json
另一个解决方案可能是构建一个单独的“地理”应用程序,它可以“服务”(我不知道如何)将数据提供给其他项目...实际上,我尝试了GeoDjango,但它似乎非常复杂,并且可能无法回答我的问题!
谢谢非常提前为您提供答案!
I come today with a design/architecture question concerning Django.
I work on several websites (hosted on the same server) which individually need geographical data (states, towns, etc.). Each project contains apps, and each app may contain models with ForeignKey
fields to Town or State.
In order to not repeat myself, I wan't to build a database to store these towns and states, and to use it through Django projects.
Django provides a straightforwards way to use several databases in the same project, declaring it in the settings.py
file and writing routers classes to hold reading and writing stuff. But that way, impossible to use select_related
statement like :
job = get_object_or_404(Jobs.objects.select_related('state__town'), user=user)
This behaviour is just natural to me (impossible to make joins between databases, from scratch)...
My questions :
- Is it a good idea to consider introducing dblinks (I don't think so...) and can Django handle it (I didn't find any docs for this part) ?
- How would you proceed, facing this situation ?
A quick and dirty solution would be to import all geo data (towns, states...) in each project database, but it's not DRY at all :( :
python manage.py loaddata geo.json
Another solution may be to build a separated "geo" app, which could "serve" (I don't know how) the data to other projects... Actually, I tried GeoDjango, but it seems to be really complex, and it probably won't answer my question !
Thank you very much in advance for your answers !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据此数据的静态程度,最简单的方法可能是在 Python 中定义这些城镇和州一次,然后在所有单独的项目中导入此定义:
然后您可以使用 charfield 指定,而不是使用外键options kwarg:
这种方法不太容易更新,并且它不记录城镇和州之间的关系(即一个城镇位于一个州),但它非常简单。
Depending upon how static this data is, the simplest way might be to just define these towns and states in Python once and then import this definition in all of your separate projects:
And then you can, instead of using a Foreign key use a charfield specifying the options kwarg:
This approach is not very easy to update, and it does not record the relationship between towns and states (i.e. A town is in one state), however it is dead simple.
您可以创建数据库视图以从其他数据库引入静态数据。然后你的模型可以指向这个数据库视图。您甚至可以在数据库视图中创建联接。
You can create a database view to bring in the static data from the other database. Then your model can point to this database view. You can even create joins within the database view.