无法使用django 1.5自定义用户类执行数据迁移

时间:2022-01-22 21:37:49

I have many applications with historic south initial migrations, which i wanted to convert to django 1.5 applicable. So i swapped out all orm['auth.User'] references in migration files with custom ones, but when i try to run those migrations i get following error:

我有很多应用程序具有历史性的南初始迁移,我想将其转换为django 1.5适用。所以我用自定义文件换出了迁移文件中的所有orm ['auth.User']引用,但是当我尝试运行这些迁移时,我得到以下错误:

Error in migration: django_notify:0001_initial KeyError: "The model 'customuser' from the app 'profiles' is not available in this migration."

迁移错误:django_notify:0001_initial KeyError:“此迁移中无法使用应用程序'配置文件'中的模型'customuser'。”

The migration in question is this: http://bpaste.net/show/2CwaYrlNifNTd5gcHUfK/

有问题的迁移是这样的:http://bpaste.net/show/2CwaYrlNifNTd5gcHUfK/

My custom User class is:

我的自定义User类是:

class CustomUser(AbstractUser):
    image = models.ImageField(_('Image Field'), upload_to='user_images')

I am also unable to convert my'profiles' app to south using convert_to_south command. I get the following error:

我也无法使用convert_to_south命令将my'profiles'应用程序转换为南方。我收到以下错误:

Creating init.py in '/Users/tejinder/Projects/basidia/apps/profiles/migrations'...

在'/ Users / tejinder / Projects / basidia / apps / profiles / migrations'中创建init.py ...

  • Added model profiles.CustomUser

    添加了模型profiles.CustomUser

    • Added M2M table for groups on profiles.CustomUser

      在profiles.CustomUser上为组添加了M2M表

    • Added M2M table for user_permissions on profiles.CustomUser

      在profiles.CustomUser上为user_permissions添加了M2M表

Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate profiles

创建0001_initial.py。您现在可以将此迁移应用于:./ manage.py迁移配置文件

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'profiles.CustomUser' which has not been installed or is abstract.

CommandError:一个或多个模型没有验证:auth.user:模型已被换出'profiles.CustomUser',它尚未安装或是抽象的。

What could have been gone wrong? Thanks in advance.

可能出了什么问题?提前致谢。

2 个解决方案

#1


12  

See this answer: Migrating existing auth.User data to new Django 1.5 custom user model?

请参阅以下答案:将现有的auth.User数据迁移到新的Django 1.5自定义用户模型?

For others that may have a similar problem starting with a custom user model:

对于从自定义用户模型开始可能有类似问题的其他人:

If you are using 'django.contrib.auth' and have a custom user model you can't run syncdb without having your custom user model includded in the installed apps. You will get this error

如果您使用的是“django.contrib.auth”并且拥有自定义用户模型,则无法在未安装的应用程序中包含自定义用户模型的情况下运行syncdb。你会收到这个错误

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'myapp.User' which has not been installed or is abstract. admin.logentry: 'user' has a relation with model myapp.User, which has either not been installed or is abstract.

CommandError:一个或多个模型没有验证:auth.user:模型已被换出'myapp.User',它尚未安装或是抽象的。 admin.logentry:'user'与模型myapp.User有关系,该模型未安装或是抽象的。

So to fix that you need to include your app containing your user model in the installed apps and now when you run syncdb it will add all the tables for your own models. So you have to convert your app to south since the tables have already been created.

因此,要解决此问题,您需要在已安装的应用程序中包含包含用户模型的应用程序,现在运行syncdb时,它将为您自己的模型添加所有表格。因此,您必须将应用程序转换为南方,因为已经创建了表格。

python manage.py syncdb
python manage.py migrate
python manage.py convert_to_south myapp

This will create 0001_initial and you get this error:

这将创建0001_initial并且您收到此错误:

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'myapp.User' which has not been installed or is abstract.

CommandError:一个或多个模型没有验证:auth.user:模型已被换出'myapp.User',它尚未安装或是抽象的。

Workaround:

python manage.py syncdb
python manage.py migrate
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake

You will still get the error above when you run convert_to_south but you can ignore it for now. The South documentation says:

当你运行convert_to_south时,你仍然会得到上面的错误,但你现在可以忽略它。南方文件说:

convert_to_south: South will automatically make and pretend to apply your first migration

convert_to_south:So​​uth将自动制作并假装应用您的第一次迁移

I think the problem is that the model validation is causing convert_to_south to error out before it pretends (--fake) to apply your first migration.

我认为问题在于模型验证导致convert_to_south在假装(--fake)应用您的第一次迁移之前出错。

So the workaround is to basically do the fake migration that got skipped.

因此,解决方法是基本上执行跳过的虚假迁移。

#2


0  

I solved this same problem the other day so I figured other people might find this useful. This happens when the migration is created before South had Django 1.5+ compatibility. I had a similar issue with using PybbM Forum App in my Django Site. The solution is to include your model and related models into the old migrations ORM. There are two ways to do this:

前几天我解决了同样的问题所以我认为其他人可能会觉得这很有用。在South具有Django 1.5+兼容性之前创建迁移时会发生这种情况。我在Django网站上使用PybbM论坛应用程序时遇到了类似的问题。解决方案是将您的模型和相关模型包含在旧迁移ORM中。有两种方法可以做到这一点:

1) You can manually type out your model in the "models" list at the bottom of each failing migration file. 2) Use one of your existing passing migrations as a template and copy the desired orm models to the end.

1)您可以在每个失败的迁移文件底部的“模型”列表中手动键入模型。 2)使用您现有的一个传递迁移作为模板,并将所需的orm模型复制到最后。

Example: http://bpaste.net/show/Pv20CM5dTrbubzFZtiRY/

*Keep in mind that you will need to also copy in any related model your custom user might need so in the case of the custom user I made for you, you would need to copy in a 'ranks.rank', 'ranks.ranktest' and 'schools.school '

*请记住,您还需要复制自定义用户可能需要的任何相关模型,因此对于我为您制作的自定义用户,您需要复制'ranking.rank','ranking.ranktest '和'schools.school'

#1


12  

See this answer: Migrating existing auth.User data to new Django 1.5 custom user model?

请参阅以下答案:将现有的auth.User数据迁移到新的Django 1.5自定义用户模型?

For others that may have a similar problem starting with a custom user model:

对于从自定义用户模型开始可能有类似问题的其他人:

If you are using 'django.contrib.auth' and have a custom user model you can't run syncdb without having your custom user model includded in the installed apps. You will get this error

如果您使用的是“django.contrib.auth”并且拥有自定义用户模型,则无法在未安装的应用程序中包含自定义用户模型的情况下运行syncdb。你会收到这个错误

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'myapp.User' which has not been installed or is abstract. admin.logentry: 'user' has a relation with model myapp.User, which has either not been installed or is abstract.

CommandError:一个或多个模型没有验证:auth.user:模型已被换出'myapp.User',它尚未安装或是抽象的。 admin.logentry:'user'与模型myapp.User有关系,该模型未安装或是抽象的。

So to fix that you need to include your app containing your user model in the installed apps and now when you run syncdb it will add all the tables for your own models. So you have to convert your app to south since the tables have already been created.

因此,要解决此问题,您需要在已安装的应用程序中包含包含用户模型的应用程序,现在运行syncdb时,它将为您自己的模型添加所有表格。因此,您必须将应用程序转换为南方,因为已经创建了表格。

python manage.py syncdb
python manage.py migrate
python manage.py convert_to_south myapp

This will create 0001_initial and you get this error:

这将创建0001_initial并且您收到此错误:

CommandError: One or more models did not validate: auth.user: Model has been swapped out for 'myapp.User' which has not been installed or is abstract.

CommandError:一个或多个模型没有验证:auth.user:模型已被换出'myapp.User',它尚未安装或是抽象的。

Workaround:

python manage.py syncdb
python manage.py migrate
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake

You will still get the error above when you run convert_to_south but you can ignore it for now. The South documentation says:

当你运行convert_to_south时,你仍然会得到上面的错误,但你现在可以忽略它。南方文件说:

convert_to_south: South will automatically make and pretend to apply your first migration

convert_to_south:So​​uth将自动制作并假装应用您的第一次迁移

I think the problem is that the model validation is causing convert_to_south to error out before it pretends (--fake) to apply your first migration.

我认为问题在于模型验证导致convert_to_south在假装(--fake)应用您的第一次迁移之前出错。

So the workaround is to basically do the fake migration that got skipped.

因此,解决方法是基本上执行跳过的虚假迁移。

#2


0  

I solved this same problem the other day so I figured other people might find this useful. This happens when the migration is created before South had Django 1.5+ compatibility. I had a similar issue with using PybbM Forum App in my Django Site. The solution is to include your model and related models into the old migrations ORM. There are two ways to do this:

前几天我解决了同样的问题所以我认为其他人可能会觉得这很有用。在South具有Django 1.5+兼容性之前创建迁移时会发生这种情况。我在Django网站上使用PybbM论坛应用程序时遇到了类似的问题。解决方案是将您的模型和相关模型包含在旧迁移ORM中。有两种方法可以做到这一点:

1) You can manually type out your model in the "models" list at the bottom of each failing migration file. 2) Use one of your existing passing migrations as a template and copy the desired orm models to the end.

1)您可以在每个失败的迁移文件底部的“模型”列表中手动键入模型。 2)使用您现有的一个传递迁移作为模板,并将所需的orm模型复制到最后。

Example: http://bpaste.net/show/Pv20CM5dTrbubzFZtiRY/

*Keep in mind that you will need to also copy in any related model your custom user might need so in the case of the custom user I made for you, you would need to copy in a 'ranks.rank', 'ranks.ranktest' and 'schools.school '

*请记住,您还需要复制自定义用户可能需要的任何相关模型,因此对于我为您制作的自定义用户,您需要复制'ranking.rank','ranking.ranktest '和'schools.school'