为现有的数据库/ sql视图创建django模型? [英] creating django model for existing database/sql view?

查看:186
本文介绍了为现有的数据库/ sql视图创建django模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在$ template_dir / sql / $ someTableName.sql文件中插入了一个视图的定义。 (创建或替换视图),所以每次运行 syncdb 时,将创建数据库视图。

I have inserted a definition for a view in $template_dir/sql/$someTableName.sql file. (create or replace view) so every time I run syncdb, the db views are created.

我可以创建在models.py一个python类访问该视图?

Can I create in models.py a python class which accesses that view?

更好的做法是使用python的原始sql功能?

Is it better practice to just use python's raw sql functionality instead?

---编辑---

另一个问题是,该视图没有主键,但是django似乎在假设将会是一个,因为我得到一个错误在呈现时捕获异常:(1054,未知列'appName_currentleagues.id'在'字段列表')但我不有这样的成员定义:

Another problem I have is that the view doesn't have a primary key, but django seems to be assuming there will be one because I get an error caught an exception while rendering: (1054, "Unknown column 'appName_currentleagues.id' in 'field list'") But I don't have such a member defined:

Class CurrentLeagues(models.Model):
        League = models.ForeignKey(League)
        class Meta:
                managed = False

所以我的猜测是django合理看到我没有在CurrentLeagues类中定义一个主键,所以Django假设有一个叫 id 。有没有办法告诉Django没有主键(因为这实际上是一个视图),或者甚至不是我的问题?

So my guess is that django reasonably sees that I don't define a primary key in the CurrentLeagues class, so Django assumes there's one called id. Is there a way to tell Django that there is no primary key (since this is actually a view), or is that not even my problem?

推荐答案

请参阅未管理模型

您像往常一样定义模型,并将 managed = False 添加到元选项: / p>

You define the model as usual and add managed = False to the meta options:

class MyModel(models.Model):
    ...
    class Meta:
         managed = False

这篇关于为现有的数据库/ sql视图创建django模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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