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

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

问题描述

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



这里是我的视图模型:

 
from django.db import models

类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。

解决方案

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



Django 1.1:请参阅发行说明



默认为True,这意味着Django将在syncdb中创建相应的数据库表,并将它们作为重置管理命令的一部分删除。也就是说,Django管理数据库表的生命周期。



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


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

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



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


测试涉及managed = False的模型,由您决定是否在测试设置中创建正确的表。



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.

here is my view models :

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: I use sqlite3.

解决方案

Try this: http://docs.djangoproject.com/en/dev/ref/models/options/#managed

managed

Options.managed

New in Django 1.1: Please, see the release notes

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

  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.

    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.

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天全站免登陆