django - 我可以在 Django 中使用数据库 View 作为模型吗?

标签 django django-migrations django-syncdb

我想使用我在数据库中创建的 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 in syncdb and remove them as part of a reset 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 when managed is False. All other aspects of model handling are exactly the same as normal.

关于django - 我可以在 Django 中使用数据库 View 作为模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/507795/

相关文章:

python - 如何通过 Django App 调用 Scrapy Spider

django - Django:是否可以过滤通过ForeignKey链接的多个模型?

python - 迁移与 forms.py 冲突

django - 测试使用南迁移的 Django 应用程序

database - Heroku Django : Running a Worker

django - Perl 中的哪个 MVC 框架有自己的独立服务器?

python - 在 Django REST ListAPI View 中对原始 SQL 查询进行分页的最佳方法?

python - makemigration 导致错误 "TypeError: expected str, bytes or os.PathLike object, not NoneType"

django - 如何使自定义 django 迁移可逆?

python - syncdb 忽略导入的模型