Temporary models in django 1.9

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

Code from the specified article that 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`')
+4
source share
2 answers

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

, "" , , . , , 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)
+1

Source: https://habr.com/ru/post/1624119/


All Articles