How do you deal with circular imports ?
See original GitHub issueFor example I have models like this :
# modules/patients/models.py
class Patient(db.Model, OwnerMixin):
__owner_backref_name__ = "patients"
id = db.Column(db.Integer, primary_key=True)
phone = db.Column(db.String)
email = db.Column(db.String)
first_name = db.Column(db.String)
last_name = db.Column(db.String)
birthday = db.Column(db.DateTime)
zip_code = db.Column(db.String)
address = db.Column(db.String)
# I would like to do this, but there's circular import....
@aggregated('claims', db.Column(db.Integer))
def unpaid_amount(self):
return db.func.count('1')
# modules/accounting/models.py
class Claim(db.Model, OwnerMixin):
__owner_backref_name__ = 'claims'
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, nullable=False)
label = db.Column(db.String, nullable=False)
amount = db.Column(db.Float, nullable=False)
ref = db.Column(db.String)
patient_id = db.Column(db.Integer, db.ForeignKey('patient.id'), nullable=False)
patient = db.relationship(
Patient,
backref=db.backref('claims', cascade="delete, delete-orphan")
)
Patient is in dedicated module “patient” and claim model is in ‘accounting’ modules with other models related to accounting. The problem is I can’t import claim model and I can’t access to claim model in patient model in order to, for example, make the sum of claims amount. I think this is some design issue, but I want to be sure if there’s any workaround before rewriting…
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Python Circular Imports
Circular imports are a specific case of circular references. Generally, they can be resolved with better code design. However, sometimes, the ...
Read more >Pythonic way to resolve circular import statements?
Is there a Pythonic way to deal with circular dependencies like this and still keep import statements at the top of the file?...
Read more >Avoiding Circular Imports in Python | by André Menck
The notable thing here is that in order to resolve this circular import, we actually had to move a class definition into another...
Read more >Python Circular Import Problem and Solutions
How to fix Python Circular Import? · Importing The Module Itself · Rename Your Working file · Avoid Circular Import Calls.
Read more >Python Type Hints - How to Fix Circular Imports - Adam Johnson
Circular imports are always annoying when they arise in Python, and type hints make them more common. Thankfully, there's a trick to add...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes this is what I’ve been up these last minutes. Many thanks. I resolved it with this solution plus :
@askz And what about @frol’s advice about providing related model as string without importing it? Didn’t it help?
@frol should it work?