在一個芹菜任務中,我需要在數據庫中創建臨時表。 Daniel Roseman在this article中解釋瞭如何創建一個。但是這個解決方案在Django 1.9中不起作用。我試圖研究Django文檔和Google,但是我找不到任何有用的東西。從提到的文章django中的臨時模型1.9
守則,在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`')
我應該說,利用克隆一個deepcopy的字段()不是一個完整的解決方案...這只是一個例子。 deepcopy()保留將複製的字段綁定回原始模型的各種數據。 –
FWIW,事實證明Field.clone()知道如何複製一個字段。答案更新,以反映這一點。 –