South data migration 'instance' error when using south freeze orm

我与影子孤独终老i 提交于 2019-11-29 22:16:01

问题


I have a south datamigration that is trying to create new objects based on data found in other models. When trying to create a new object for the given 'destination' model I keep getting:

Cannot assign "<ContentType: ContentType object>": "Publishing.content_type" must be a "ContentType" instance.

It seems that there is something wrong with the 'instance' when accessed via the South freeze ORM, e.g.:

ContentType = orm['contenttypes.ContentType']
content_type_kwargs = {
    'model': ContentModel._meta.module_name, 
    'app_label': ContentModel._meta.app_label, }
content_type = ContentType.objects.get(**content_type_kwargs)

# further down

publishing_kwargs = {
    'site': Site.objects.get_current(),
    'publishing_type': publishing_type,
    'start': start,
    'content_type': content_type, 
    'object_id': content_object.id, }

publishing = orm.Publishing(**publishing_kwargs) # Produces the error above

Now I've verified many times that the content_type is in fact an instance of ContentType -- yet somehow django doesn't think so.

  • Is there a difference between the 'freezed', south orm version of the instance and the native django one?
  • What else might this possibly be?

回答1:


It's due to the way that South handles models. You must freeze any model you need to work with in your migration. The models in the app where the migration resides get frozen automatically; anything else you must freeze manually:

python manage.py schemamigration --auto yourapp --freeze contenttypes

If you have multiple apps you need to freeze, repeat the --freeze argument as many times as necessary:

python manage.py schemamigration --auto yourapp --freeze contenttypes --freeze someotherapp ...

One other thing. When you access these additional frozen models you have to use the old-style South API:

orm['contenttypes.contenttype'].objects.all()

Something like orm.ContentType won't work.



来源:https://stackoverflow.com/questions/6211027/south-data-migration-instance-error-when-using-south-freeze-orm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!