我想使用我在数据库中创建的 View 作为我的 django View 的源。
如果不使用自定义 sql,这可能吗?
******13/02/09 更新***********
就像许多答案所建议的那样,您可以在数据库中创建自己的 View ,然后通过在 models.py 中定义它来在 API 中使用它。
一些警告:
- manage.py syncdb 将不再工作
- View 的名称开头需要与所有其他模型(表)相同,例如,如果您的应用名为“thing”,那么您的 View 需要称为 thing_$viewname
最佳答案
只是为遇到此问题(来自 Google 或其他任何问题)的人更新...
目前 Django 有一个简单的“正确方法”来 define model without managing database tables :
Options.managed
Defaults to
True
, meaning Django will create the appropriate database tables insyncdb
and remove them as part of areset
management command. That is, Django manages the database tables' lifecycles.If
False
, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference whenmanaged
isFalse
. All other aspects of model handling are exactly the same as normal.
关于django - 我可以在 Django 中使用数据库 View 作为模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/507795/