django的临时模特

时间:2016-01-13 14:04:30

标签: django django-models django-orm

在一个芹菜任务中,我需要在数据库中创建临时表。在this article丹尼尔罗斯曼解释了如何创建一个。但是这个解决方案在Django 1.9中不起作用。我试图调查Django文档和谷歌,但我找不到任何有用的东西。

来自Django 1.8中提到的文章的代码:

from django.db import models, cursor
from django.contrib.contenttypes.management import update_contenttypes
from django.core.management import call_command

class TempCustomerAddress(models.Model):
    address = models.ForeignKey('accounts.Address')
    legacy_id = models.CharField(max_length=12, unique=True)

    class Meta:
        app_label = 'utils'


class Command(NoArgsCommand):

    def handle_noargs(self, **options):
        models.register_models('utils', TempCustomerAddress)
        models.signals.post_syncdb.disconnect(update_contenttypes)
        call_command('syncdb')

        # ... do importing and stuff referring to TempCustomerAddress ...

        cursor = connection.cursor()
        cursor.execute('DROP TABLE `utils_tempcustomeraddress`')

2 个答案:

答案 0 :(得分:3)

在django 1.9中,你实际上不需要注册任何东西。你只需要像models.py一样创建模型,就是这样。您只需要确保它不在models.py文件中,因为它将是永久模型。 此示例假定您已经运行了所有迁移。

from django.db import models, cursor
from django.contrib.contenttypes.management import update_contenttypes
from django.core.management import call_command

class TempCustomerAddress(models.Model):
    address = models.ForeignKey('accounts.Address')
    legacy_id = models.CharField(max_length=12, unique=True)

    class Meta:
        app_label = 'utils'


class Command(NoArgsCommand):

    def handle_noargs(self, **options):
        with connection.cursor() as cursor:
            cursor.execute('DROP TABLE IF EXISTS utils_tempcustomeraddress')
            cursor.execute('''
                CREATE TABLE utils_tempcustomeraddress (
                    id INTEGER PRIMARY KEY NOT NULL,
                    address_id REFERENCES accounts_address (id),
                    legacy_id VARCHAR(12) UNIQUE
                );
            '''
            # ... do importing and stuff referring to TempCustomerAddress ...

            cursor.execute('DROP TABLE `utils_tempcustomeraddress`')

答案 1 :(得分:1)

我需要创建一个源自"永久"的临时模型。模型,并使用临时表存储,以避免污染永久表的表。经过大量的讨论,其中包括一篇与http://localhost:63952/Api/Business_Register相关的文章,它为Django 0.96提供了一些较新的资料,而一些Django 1.2基于迁移技术整合到Django中我终于找到了一个食谱适用于Django 2.0。

首先,我需要使用Meta:

显式指定数据库表名
model_name = re.sub('[@.]', '_', 'some_string')

class Meta:
    app_label = original_model._meta.app_label
    #
    # Use the explicit name for the database table.
    #
    db_table = '"' + model_name.lower() + '"'

然后我通过从原始文件复制我需要的东西来创建Model类:

attr = {'__module__': __name__, 'Meta': Meta}
local_fields = [field.name for field in original_model._meta.local_fields]
for field in original_model._meta.fields:
    #
    # Clone only the fields which we need, not forgetting that we only
    # want one primary key.
    #
    clone = field.clone()
    if field.name in local_fields:
        local_fields.remove(field.name)
    else:
        clone.primary_key = False
    if not isinstance(field, (db_models.AutoField, db_models.OneToOneField, db_models.ManyToManyField)):
        attr[field.name] = clone
new_model = type(model_name, (db_models.Model,), attr)

困难的部分是追踪如何为模型创建新表。一旦找到,答案很简单:

from django.db import connection

with connection.schema_editor() as schema_editor:
    schema_editor.create_model(new_model)