如何使用sqlite3在django中迁移数据

时间:2014-11-09 21:50:03

标签: python django django-models sqlite django-migrations

按照django网页上的官方步骤后,我一直收到此错误:

  

返回Database.Cursor.execute(self,query,params)

     

django.db.utils.OperationalError:数据库被锁定

我的问题是我正在尝试将数据从旧列迁移到新列:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

def move_names(apps, schema_editor):
    Comment = apps.get_model("article", "Comment")
    for comment in Comment.objects.all():
        try:
            comment.first_name, comment.second_name = comment.name.split(" ")
        except:
            comment.first_name, comment.second_name = comment.name, ""
        comment.save()

def backwards(apps, schema_editor):
    for comment in Comment.objects.all():
        comment.name = ""
        comment.save()

class Migration(migrations.Migration):

    dependencies = [
        ('article', '0007_auto_20141109_2005'),
    ]

    operations = [
        migrations.RunPython(move_names),
        #migrations.RunPython(backwards),
    ]

0 个答案:

没有答案