4

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?
Daryl
  • 1,469
  • 5
  • 18
  • 30
  • Daryl, I am facing the same issue goo.gl/I7Jj6 Did you manage to resolve it? I am now trying to use content_type & object_id to create a new instance in the datamigration but get the exact error that you have posted above. – Chantz Aug 13 '11 at 18:09

1 Answers1

8

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.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks for the answer @chrisdpratt, though this is more like checking everything is 'plugged in', so to speak. The 'trick' that I'm facing, is that the 'instances' provided by South's orm, e.g. orm['contenttypes.contenttype'].objects.get(pk=1) != ContentType.objects.get(pk=1) -- at least when used as an argument to create another model instance, such as Publishing above. Its weird, but its got me stuck. – Daryl Jun 03 '11 at 00:04
  • That's a weird one indeed. Everything you use must be from the South ORM, but once that condition is satisfied, everything should just work. I've ever only seen that particular error when trying to import some external model and use that instead of freezing it and getting it through the ORM. Maybe you should contact the South team; you might have stumbled upon some bug. – Chris Pratt Jun 03 '11 at 14:24
  • Thanks @chrispratt, I've lodged an issue with South http://south.aeracode.org/ticket/781 – Daryl Jun 06 '11 at 00:14