My data migration references a function that takes an object of the current model as a parameter, and then tries to create an object from another table (in the same app) through a foreign key. The creation of that object fails with a Value Error stating that the object passed as an argument is not an instance of the expected class (although it is). It seems like there is no link between the two tables.
Here is the data migration.
for ranking in orm.Ranking.objects.all():
find_matching_domains(ranking)
find_matching_domains
is declared in another app and imported in the data migration. It fails when trying to create an dictionary object.
dico = CategoryDict(category=ranking.category,
key="url_dict",
value=url_dict)
with the following error:
File "...", line 175, in save_url_dict value=url_dict)
File ".../lib/python2.6/site-packages/django/db/models/base.py", line 355, in __init__ setattr(self, field.name, rel_obj)
File ".../lib/python2.6/site-packages/django/db/models/fields/related.py", line 366, in __set__ self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "<Category: Category object>": "CategoryDict.category" must be a "Category" instance.
I have frozen the corresponding schemas in the migration but this does not have effect.
I use Django 1.4.3.
Actually it turns out that data migrations are not appropriate to execute methods from a project to populate the database. Instead of writing the migration I have written an admin interface method that allow to execute my code from the admin interface.