Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:30:16+00:00 2026-05-26T16:30:16+00:00

I have a model for categories, which contains a circular foreign key. I dumped

  • 0

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')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:30:17+00:00Added an answer on May 26, 2026 at 4:30 pm

    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:

    class Migration(DataMigration):
    
        def forwards(self, orm):
            #~ Disable foreign key checks during fixture loading
            from django.db import connections, DEFAULT_DB_ALIAS
            connection = connections[DEFAULT_DB_ALIAS]
            if 'mysql' in connection.settings_dict['ENGINE']:
                cursor = connection.cursor()
                cursor.execute('SET foreign_key_checks = 0')
    
            #~ Load fixture
            from django.core.management import call_command
            call_command('loaddata', 'categories_fixture.json', verbosity=0)
    
            #~ Enable foreign key checks after fixture loading
            if 'mysql' in connection.settings_dict['ENGINE']:
                cursor = connection.cursor()
                cursor.execute('SET foreign_key_checks = 1')
            connection.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which models all categories and they can be ordered hierarchically.
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a Product Model from the database which I display on the View,
I have a model category which contains a field called category.parent_id which is used
I have a data model in which I have an Item that can belong
I have a list of categories and sub categories which is passing from controller
I have Product model and it has many categories with a has_many :through association
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I have a set of data that models a hierarchy of categories. A root
im trying to make Categories and Subcategories, im checking this models but i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.