Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 991
Averin Maxim Avatar asked May 26 '26 08:05

Averin Maxim


1 Answers

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):
...
like image 77
pharingee Avatar answered May 30 '26 10:05

pharingee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!