Ticket #781 (assigned defect)

Opened 4 years ago

Last modified 2 years ago

Model Instances not valid when used as arguments for creating another object

Reported by: daryl@… Owned by: andrew
Priority: major Milestone: 1.0
Component: migrations Version: 0.7.3
Keywords: Cc:


I'm having trouble with creating new objects in a datamigration.

When using object instances from the South orm, e.g. content_type_instance = ormcontenttypes.ContentType?.objects.get(ct_kwargs)

to create another object:

publishing_kwargs = {

'site': site if isinstance(site, Site) else Site.objects.get(id=site.id),
'publishing_type': publishing_type_instance,
'start': start,
'content_type': content_type_instance,
'object_id': content_object.id, }

publishing = orm.Publishing(publishing_kwargs)

I get a ValueError?:

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

See also:




0006_convert_remaining_content_objects.py (43.4 KB) - added by daryl@… 4 years ago.
datamigration file
0006_convert_remaining_content_objects.2.py (44.9 KB) - added by daryl@… 4 years ago.
updated data migration with hopeful fixes
0006_output.txt (30.9 KB) - added by daryl@… 4 years ago.
print debug output of migration

Change History

Changed 4 years ago by daryl@…

datamigration file

comment:1 Changed 4 years ago by andrew

  • Status changed from new to assigned
  • Milestone set to 1.0

This is because the south ORM classes of models are _not the same_ as the main Django classes for your models - they won't compare equal, and you can't feed instances of one type into a ForeignKey? of another type.

Unfortunately, knowing which kind a ForeignKey? is can sometimes be tricky. Have you tried both with and without the orm version of ContentType??

Changed 4 years ago by daryl@…

updated data migration with hopeful fixes

Changed 4 years ago by daryl@…

print debug output of migration

comment:2 Changed 4 years ago by daryl@…

Thanks @andrew, I've reattached an updated version of the file whereas I attempt to get the 'native' instances for use in creating new objects.

With the 'Publishing' object that I'm trying to create, you'll notice at the bottom that I try the 3 possible notations for creating objects, two of them via the south orm, and the third via native django.

Each of them returns the instance error, however, curiously, complaining about different objects, i.e. sometimes about the content_type, and sometimes about the 'site'.

I've included the output from the print statements found in the migration.

I had a 'feeling' that orm.ContentType? != ContentType? -- yet, this issue seems to be baffling.

comment:3 follow-up: ↓ 4 Changed 4 years ago by daryl@…

Uhhh, nevermind. Programmer error :)

comment:4 in reply to: ↑ 3 Changed 4 years ago by brandon.ivey@…

Replying to daryl@…:

Uhhh, nevermind. Programmer error :)

Daryl, what was the error, and how did you get around it? Having similar problems with migrations involving contenttypes.

comment:5 Changed 2 years ago by dakinsloss@…

I am having the same issue here. It appears that in fields/related.py, it gets checked that the model being assigned as the foreign key is of the instance type the same as the foreign key in the model definition (e.g.
class Location:
class Party:
location = models.ForeignKey?(Location)

For some reason, in south self.field.rel.to is ModelBase? (not Location) with self as Party and field location.

Any suggested fixes?

Note: See TracTickets for help on using tickets.