我有一个空白的MySQL数据库,我刚刚创建.south在我的INSTALLED_APPS.
我跑:
$ ./manage.py syncdb
...
Creating table some_app_table
You just installed Django's auth system, which means you don't have any superusers defined.
...
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
Synced:
> django.contrib.auth
> django.contrib.contenttypes
...
Not synced (use migrations):
- myapp
...
Run Code Online (Sandbox Code Playgroud)
$ ./manage.py schemamigration myapp --initial
+ Added model myapp.Model
...
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate myapp
Run Code Online (Sandbox Code Playgroud)
$ …Run Code Online (Sandbox Code Playgroud) 对于我的django1.4应用程序,我正在尝试使用south来进行数据迁移.我正在使用postgres8.3
我有一个MyCategory模型,我需要添加一个名为creatordjango.contrib.auth.models.User 的字段
我修改了类并添加了字段
from django.contrib.auth.models import User
class MyCategory(models.Model):
name=models.CharField(unique=True,max_length=50)
description=models.TextField(blank=True)
creator = models.ForeignKey(User,null=True)#added
slug=models.SlugField(editable=False)
Run Code Online (Sandbox Code Playgroud)
我跑了 python manage.py schemamigration myapp --auto
输出+ Added field creator on myapp.MyCategory
并创建了一个文件0003_auto__add_field_mycategory_creator.py
然后我希望所有旧记录都将webapp的超级用户作为创建者.所以我尝试了数据迁移
我跑了 python manage.py datamigration myapp add_creator
这输出了一个文件add_creator.py,我在其中实现了如下的forwards方法.(后来我添加了一个print stmt用于调试目的)
def forwards(self, orm):
from django.contrib.auth.models import User
suser = User.objects.filter(is_superuser=True)[0]
# I also tried User.objects.get(is_superuser=True)
print 'suser=',suser,'of type=',type(suser)
for category in orm.MyCategory.objects.all():
category.creator = suser
category.save()
Run Code Online (Sandbox Code Playgroud)
然后我运行了migrate命令
python manage.py migrate myapp
这个输出一个 ValueError
Running migrations for …Run Code Online (Sandbox Code Playgroud) 我正在使用南方的Django Web应用程序进行数据库迁移.我对南方很新,还有django.我尝试使用南方的官方教程,但它失败了一个例外:AttributeError:'Options'对象没有属性'index_together'.我像这样运行南命令:
python manage.py schemamigration southtut --initial
Run Code Online (Sandbox Code Playgroud)
southtut模型是这样的:
class Knight(models.Model):
name = models.CharField(max_length=100)
of_the_round_table = models.BooleanField()
Run Code Online (Sandbox Code Playgroud)
我的项目模型是这样的:
class Author(models.Model):
name = models.CharField(max_length=64)
authorId = models.CharField(max_length=32)
def __unicode__(self):
return self.name
class Meta:
db_table="Author"
class Video(models.Model):
videoId = models.CharField(max_length=32)
videoUrl = models.URLField(max_length=200)
author = models.ForeignKey(Author, null=True, related_name="videos", on_delete=models.SET_NULL)
class Meta:
db_table="Video"
class User(models.Model):
token = models.CharField(max_length=50, null=True)
favs = models.ManyToManyField(Video, related_name="fans", db_table="VideoUserR")
class Meta:
db_table = "User"
Run Code Online (Sandbox Code Playgroud)
我得到的整个错误信息如下:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv) …Run Code Online (Sandbox Code Playgroud) 在学习南方的过程中,我犯了一些错误,我想重申一下。这是其中之一:
# migrate --list
(*) 0002_auto__add_lesson #add the table
(*) 0003_auto__del_lesson #dammit, table uses wrong db engine so delete it
(*) 0004_auto__add_lesson #recreate the table
Run Code Online (Sandbox Code Playgroud)
显然,我可以只运行第 4 步,如果它们被加星标,则不会应用第 2 步和第 3 步:
./migrate my_app 0004
Run Code Online (Sandbox Code Playgroud)
但是由于第2步和第3步是多余的,不需要运行,是否可以简单地删除它们?另外,我应该补充一点,我担心有人可能会运行 migrate ,然后会删除表(和任何数据),然后重新创建它...
提前致谢,