如何共享Django模型? [英] How to share a Django model?

查看:46
本文介绍了如何共享Django模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先使用DJango作为快速/肮脏的数据访问方法是愚蠢的吗?我们有一个后端流程,该流程将提供将使用django实现的GUI,并且我们认为我们可以在它们之间共享模型.我们不在乎性能,后端过程只需要一种快速的方法即可将某些东西扔到数据库中.

First is it dumb to use DJango as a quick/dirty data access method? We have a backend process that will be feeding a GUI which will be implemented with django, and we figured we could just share a model between them. We dont care about performance the backend process just needs a quick way to throw something in a DB.

假设不傻,我们如何在UI和后端流程之间共享模型?

Assuming is not dumb how to we go about sharing a model between a UI and Backend process?

现在,我们已经定义了模型",我们只是不知道它应该驻留在哪里.我们尝试将其放在模型包的后端过程中,但是由于它是一个应用程序,因此我们无法真正为其生成SQL.

Right now we have the "model" defined we just dont know where it should reside. We tried putting it in the backend process in a model package, but since its an app we can't really generate the SQL for it.

我们还尝试仅创建一个models.py并导入我们的模型,但它不会生成任何sql.

We also tried just creating a models.py and importing our models but it doesn't generate any sql.

在幕后,我知道它使用[app_name] _ [table/model]作为数据库表名称,那么我们是否试图做一些我们不应该做的事情?

Behind the scenes I know it uses[app_name]_[table/model] for the db table name, so are we trying to do something we shouldn't?

真的想知道使用共享Django模型的正确方法.任何帮助将不胜感激.

Really would like to know the correct way to use a shared django model. Any help would be appreciated.

我目前已经在ui之外定义了模型,我试图找出如何在应用程序"中引用它们,以便在我调用manage.py sql应用程序时会生成我的模型.

I currently have defined the model outside of the ui, and I am trying to find out how to reference them in the "app" so that when I call manage.py sql app it generates my model.

推荐答案

只要在设置文件中连接到有问题的数据库,并且模型定义表中存在的字段,您只需要设置 db_table 元选项以引用实际的表名,并且通过ORM生成的SQL应该返回结果/尝试更新表.

As long as you're hooked up to the database in question in your settings file, and the model defines fields that exist on the table, you just need to set the db_table meta option to refer to the actual table name and the SQL generated via the ORM should return results / attempt to update the table.

http://docs.djangoproject.com/en/dev/ref/models/options/#db-table

然后,只需导入该模型并使用ORM.正如Ignacio所链接的那样,您必须为django指定 os.environ ['DJANGO_SETTINGS_MODULE'] 才能知道要为模型使用哪个设置文件.

Then, it's just a matter of importing that model and using the ORM. As Ignacio linked to, you will have to specify os.environ['DJANGO_SETTINGS_MODULE'] for django to know which settings file to use for your model.

PS:那篇文章很棒,我不知道设置环境的捷径!

PS: Awesome stuff in that post, I didn't know there was an shortcut for setting up the environment!

这篇关于如何共享Django模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆