Django的临时模型 [英] Temporary models in django

查看:48
本文介绍了Django的临时模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一项芹菜任务中,我需要在数据库中创建临时表。 Daniel Roseman在本文中解释了如何创建一个。但是此解决方案在Django 1.9中不起作用。我试图研究Django文档和Google,但找不到任何有用的东西。



上述文章在Django 1.8中有效的代码:


django.db导入模型中的

 ,django.contrib.contenttypes.management导入中的光标
update_contenttypes django.core.management导入中的
call_command

class TempCustomerAddress(models.Model):
地址= 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')

#...进行导入并引用TempCustomerAddress的东西...

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


解决方案

在Django 1.9中,您实际上不需要注册任何东西。您只需按照与 models.py 中相同的方式创建模型即可。您只需要确保它不在 models.py 文件中,因为它将是永久模型。
此示例假定您已经运行了所有迁移。



来自django.db导入模型,光标
从django.contrib.contenttypes.management导入update_contenttypes
从django.core.management导入call_command

class TempCustomerAddress(models.Model):
地址= 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('如果存在则删除表utils_tempcustomeraddress')
cursor.execute('''
CREATE TABLE utils_tempcustomeraddress(
id INTEGER PRIMARY KEY NOT NULL,
address_id REFERENCES accounts_address(id),
legacy_id VARCHAR(12)唯一
);
'''
#...进行导入和引用TempCustomerAddress的东西...

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


In one celery task I need to create temporary table in database. In this article Daniel Roseman explained how to create one. But this solution does not work in Django 1.9. I tried to look into Django docs and Google but I was unable to find anything useful.

Code from mentioned article which worked in 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`')

解决方案

In django 1.9 you actually don't need to register anything. You just create model the same way as in models.py and that's it. You only need to make sure that it is not in models.py file because than it will be permanent model. This example assumes that you already ran all migrations.

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`')

这篇关于Django的临时模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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