Averin Maxim
Averin Maxim

Reputation: 373

Django circular import and model issue

I have two Django models.py in two different apps.

processor app models.py

from address.models import Address
...defining clasess
class Displayble(models.Model):
 # It has no DB fields

address app models.py

from processor.models import Displayable
class Address(models.Model, Displayble):
...some fields, stored in DB

Is moving Dispalyble class to another file is the only option to resolve this dependency?

Upvotes: 3

Views: 3907

Answers (1)

pharingee
pharingee

Reputation: 501

Import the Address model with django's apps.get_model. https://docs.djangoproject.com/en/1.11/ref/applications/#django.apps.apps.get_model.

In your processor app models.py replace

from address.models import Address
...defining clasess
class Displayble(models.Model):
# It has no DB fields

With

from django.apps import apps
Address = apps.get_model(app_label='address', model_name='Address')
....go ahead and use Address as though imported
class Displayable(models.Model):
...

Upvotes: 9

Related Questions