在Django中使用数据库视图 [英] use database view in Django

查看:671
本文介绍了在Django中使用数据库视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到前一个问题我可以在django中使用数据库视图作为模型,并在我的应用程序中尝试,但这不工作。我手动创建一个名为vi_topics的视图,并且具有id列。但是我总是得到关于没有这样的列:vi_topics.id,即使我显式添加id字段。

I saw the former question can i use a database view as a model in django and try it in my app,but that's not work.I create a view named "vi_topics" manually and it had "id" column。But I always got the error about "no such column: vi_topics.id",even I add "id" field explicitly.

这是我的浏览模式:


from django.db import models

class Vitopic(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    author_name = models.CharField(max_length=200)
    author_email = models.CharField(max_length=200)
    view_count = models.IntegerField(default=0)
    replay_count = models.IntegerField(default=0)
    tags = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'vi_topics'

btw:我使用sqlite3。

btw: I use sqlite3.

推荐答案

尝试这样:
http://docs.djangoproject.com/en/dev/ref/models/options/#managed


管理

managed

Options.managed

Options.managed

Django 1.1:请参阅发行说明

New in Django 1.1: Please, see the release notes

默认为True,表示Django将在syncdb中创建适当的数据库表,并将其作为reset management命令的一部分删除。也就是说,Django管理数据库表的生命周期。

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.

如果为False,则不会为此模型执行数据库表创建或删除操作。如果模型表示已经通过其他方法创建的现有表或数据库视图,这将非常有用。这是管理为False时唯一的区别。模型处理的所有其他方面与正常情况完全相同。这包括

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. This includes


  1. 如果您没有声明,则将自动主键字段添加到模型中。为避免混淆以后的代码阅读器,建议您在使用非托管模型时,从数据库表中指定正在建模的所有列。

  2. 如果具有managed = False的模型包含ManyToManyField指向另一个非托管模型,那么多对多连接的中间表也将不会被创建。但是,将创建一个托管模式和一个非托管模型之间的中间表。

  1. Adding an automatic primary key field to the model if you don't declare it. To avoid confusion for later code readers, it's recommended to specify all the columns from the database table you are modeling when using unmanaged models.
  2. If a model with managed=False contains a ManyToManyField that points to another unmanaged model, then the intermediate table for the many-to-many join will also not be created. However, a the intermediary table between one managed and one unmanaged model will be created.

如果需要更改此默认行为,请将中间表创建为显式模型(根据需要使用托管集),并使用ManyToManyField.through属性使关系使用您的自定义模型。

If you need to change this default behavior, create the intermediary table as an explicit model (with managed set as needed) and use the ManyToManyField.through attribute to make the relation use your custom model.

涉及具有managed = False的模型的测试,由您来确保在测试设置中创建正确的表。

For tests involving models with managed=False, it's up to you to ensure the correct tables are created as part of the test setup.

这篇关于在Django中使用数据库视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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