Postgres 视图的 Django 模型
Django model for a Postgres view
编辑: 我的问题似乎有些混乱。该模型适用于我在迁移 0009 中创建的 Postgres view。我的印象是 Django 不会为具有 managed = False
选项的模型生成迁移。但是,它仍在尝试创建它。
此外,我将 Django 1.8 与 Python 3.4 一起使用。
我在为 Postgres 视图创建 Django 模型时遇到问题,使用这些链接作为指南: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。但是,即使这样,它也会创建一个迁移,为视图的模型添加 table。
到目前为止,这是我的代码:
foo/models.py
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
# -*- 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 会为您的应用中每个新添加的 table 创建一个迁移,无论它是否是托管模型。然而,当您使用 managed=False
设置时,有一个非常重要和微妙的区别。由此产生的迁移是一个虚拟条目。它根本不执行任何 SQL。
要确认这一点,请添加一个不受管理的新模型
class Dummy(models.Model):
something = models.IntegerField()
class Meta:
managed = False
现在,当您执行 makemigrations
后跟 sqlimigrate *myapp* *migration_number*
时,您会发现它不会产生任何 sql。
另一方面,如果您确实发现 Django 正在尝试为您创建一个 table,这通常意味着您之前已经存在相同的模型,但当时该模型已被管理。要确认这一点,请在您的 migrations
文件夹中搜索 VirtualTotal
,这是相关模型的名称。
编辑: 我的问题似乎有些混乱。该模型适用于我在迁移 0009 中创建的 Postgres view。我的印象是 Django 不会为具有 managed = False
选项的模型生成迁移。但是,它仍在尝试创建它。
此外,我将 Django 1.8 与 Python 3.4 一起使用。
我在为 Postgres 视图创建 Django 模型时遇到问题,使用这些链接作为指南: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。但是,即使这样,它也会创建一个迁移,为视图的模型添加 table。
到目前为止,这是我的代码:
foo/models.py
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
# -*- 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 会为您的应用中每个新添加的 table 创建一个迁移,无论它是否是托管模型。然而,当您使用 managed=False
设置时,有一个非常重要和微妙的区别。由此产生的迁移是一个虚拟条目。它根本不执行任何 SQL。
要确认这一点,请添加一个不受管理的新模型
class Dummy(models.Model):
something = models.IntegerField()
class Meta:
managed = False
现在,当您执行 makemigrations
后跟 sqlimigrate *myapp* *migration_number*
时,您会发现它不会产生任何 sql。
另一方面,如果您确实发现 Django 正在尝试为您创建一个 table,这通常意味着您之前已经存在相同的模型,但当时该模型已被管理。要确认这一点,请在您的 migrations
文件夹中搜索 VirtualTotal
,这是相关模型的名称。