用于Postgres视图的Django模型 [英] Django model for a Postgres view
问题描述
编辑:我的要求似乎有些困惑。该模型适用于在迁移过程中创建的 Postgres视图 0009。我的印象是,如果Django具有 managed = False
选项,则它不会为模型生成迁移。但是,它仍在尝试创建它。
There seems to be some confusion about what I'm asking. That model is for the Postgres view that I created in migration 0009. I was under the impression that Django won't generate a migration for a model if it has the managed = False
option. However, it's still trying to create it.
此外,我正在将Django 1.8与Python 3.4一起使用。
Also, I'm using Django 1.8 with Python 3.4.
使用这些链接作为指南时,我无法为Postgres视图创建Django模型: drdaeman 和eceppda的答案能否在django中将数据库视图用作模型。我还在 Django的API文档。但是,即使这样,它仍会创建一个迁移,为视图的模型添加一个表。
I'm having trouble creating a Django model for a Postgres view, using these links as a guide: drdaeman and eceppda's answer in Can I use a database view as a model in django. I also looked up the Options.managed entry in Django's API docs. However, even with this, it's creating a migration that adds a table for the view's model.
到目前为止,这是我的代码:
This is my code so far:
class RelevantModel(models.Model):
rebate_pool_total = models.OneToOneField('foo.VirtualTotal', null=True)
total = models.DecimalField(null=True, decimal_places=2, max_digits=32)
class VirtualTotal(models.Model):
relevant_model = models.ForeignKey('foo.RelevantModel')
total = models.DecimalField(null=True, decimal_places=2, max_digits=32)
class Meta:
managed = False
foo / migrations / 0009_add_foo_view.py
foo/migrations/0009_add_foo_view.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('foo', '0008_previous_migration'),
]
sql = """
create VIEW foo_virtualtotal AS
SELECT rest of view...
"""
operations = [
migrations.RunSQL('DROP VIEW IF EXISTS foo_virtualtotal;'),
migrations.RunSQL(sql)
]
我在做什么错?
推荐答案
Django会为您应用中每个新添加的表创建迁移,无论它是否为托管模型。但是,当使用 managed = False
设置时,有一个非常重要且细微的区别。结果迁移是一个虚拟条目。
Django does create a migration for each newly added table in your app regardless of whether it's a managed model or not. However there is a very important and subtle difference when you use the managed=False
setting. The resultant migration is a dummy entry. It does not execute any SQL at all.
要确认此操作,请添加不受管理的新模型
To confirm this add a new model that is unmanaged
class Dummy(models.Model):
something = models.IntegerField()
class Meta:
managed = False
现在当您执行 makemigrations
后接着执行 sqlimigrate * myapp * * migration_number *
,您会发现它不产生任何sql。
now when you do makemigrations
followed by sqlimigrate *myapp* *migration_number*
you will see that it doesn't produce any sql.
另一方面,您会发现Django正在尝试为您创建表,这通常意味着您早先拥有相同的模型,但是在管理模型时已经存在。要确认这一点,请在您的 migrations
文件夹中搜索 VirtualTotal
,这是相关模型的名称。
If on the other hand, you do find that Django is trying to create a table for you, that usually means that you had the same model in existence earlier but at the time the model was managed. To confirm this, search your migrations
folder for VirtualTotal
which is the name of the model in question.
这篇关于用于Postgres视图的Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!