user2233706
user2233706

Reputation: 7207

Django: getting rid of circular dependency

I have two apps, commonapp and app1.

Here's commonapp/models.py:

from django.db import models
#from app1.models import SpecificFields

# Create your models here.
class CommonFields(models.Model):
    a = models.IntegerField(default = 0)

    class Meta:
        abstract = True

class SomeFields(models.Model):
#    a = models.ForeignKey(SpecificFields)
    a = models.ForeignKey('app1.models.SpecificFields')

and here's app1/models.py:

from django.db import models
from commonapp.models import CommonFields

# Create your models here.
class SpecificFields(CommonFields):
    a2 = models.IntegerField(default=0)

When I try to run the SQL from either app1 or commonapp, I get the following error:

$ python manage.py sql commonapp
CommandError: One or more models did not validate:
commonapp.somefields: 'a' has a relation with model app1.models.SpecificFields,
which has either not been installed or is abstract.

I realize this is an issue of a circular dependency. Others have suggested of specifying the path to a class as a string instead of the actual class, but that does not work. I also cannot specify a string as the base class name in the derived class.

Is such a circular dependency possible without refactoring my models?

Upvotes: 2

Views: 4219

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Import app1.models somewhere between CommonFields and SomeFields.

Upvotes: 6

Related Questions