South data migration 'instance' error when

2019-02-10 21:03发布

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条回答
男人必须洒脱
2楼-- · 2019-02-10 21:31

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.

查看更多
登录 后发表回答