当Django不是Django模型时,是否可以在数据库中使用表? [英] Is it possible to use a table in the database when it is NOT a Django model?

查看:216
本文介绍了当Django不是Django模型时,是否可以在数据库中使用表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从应用程序数据库中的表中获取不是应用程序中的模型的查询器?

Is it possible get a queryset from a table in the app database that is NOT a model in the app?

如果我有一个不是名为cartable的表格,在概念上我想这样做:

If I have a table that is not a model named "cartable", conceptually, I want to do this:

myqueryset = cartable.objects.all()

有没有一个比较容易的方法呢?谢谢!

Is there a relatively easy way to do this? Thanks!

推荐答案

为此,您需要创建一个类(而不是模型),使用原始SQL的方法。您应该在这里看到更多详细信息: https://docs.djangoproject.com/en/1.9/topics/db/sql/#executing-custom-sql-directly

To do so you would need to create a class (not a model), with methods that use raw SQL. You should see more details here on how to do so: https://docs.djangoproject.com/en/1.9/topics/db/sql/#executing-custom-sql-directly

请请注意,您将必须手动创建具有正确属性的对象。

Please note that you will have to manually create the object with the right properties afterwards.

如果您想在没有模型的情况下使用Django ORM,我认为不可能。然而,您可以在单独的应用程序中创建与您的数据库匹配的模型,并且不会为其创建迁移,以确保不会意外修改数据库。

If you wanted to use Django ORM without the models, I don't think it is possible. You could however create a model that matches your db in a separate app and never create migrations for it to ensure you don't accidentally modify the DB.

这篇关于当Django不是Django模型时,是否可以在数据库中使用表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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