I have a model for categories, which contains a circular foreign key. I dumped all the data from this model and I created a data migration with django-south for loading them into a different DBMS, but I having a lot of problem doing it, because of this circular dependency.
This is the model I’m referring:
class Category(MPTTModel):
name = models.CharField(_('name'), max_length=50, unique=True)
parent = models.ForeignKey('self', null=True, blank=True, related_name='categories')
description = models.TextField(_('description'), blank=True, null=True)
created_on = models.DateTimeField(auto_now_add = True, default=date.today())
updated_on = models.DateTimeField(auto_now = True, default=date.today())
def __unicode__(self):
return "%s" %(self.name)
class Meta:
verbose_name = _('category')
verbose_name_plural= _('categories')
Thanks to this post I could find a solution. Temporarily disable foreign key checks while you load in data is the best feasible solution for this issues.
Since Django doesn’t provide a way to do this, we should execute raw sql code.
So, I created a data migration with django-south and the rest is in the code below: