If I have "a.py"
from google.appengine.ext import db
class A(db.Model):
db.ReferenceProperty(b.B)
...other stuff
and another file "b.py"
from google.appengine.ext import db
class B(db.Model):
db.ReferenceProperty(a.A)
...other stuff
It would appear that Python simply does not allow circular dependencies. Normally I guess you would alter the code such that the two classes actually can resolve themselves without importing one another directly. Perhaps by consolidating their reference to one another through a third intermediary? But I can't just use a normal intermediary class, as all classes would ultimately need to be persisted to the database? Is there any correct solution to structuring the above code such that it works?
I have a feeling that I am going to get a lot of "bad smelling code", "decouple", "bad design", etc comments. So I ask that if you say that, please illustrate what you would do with an actual example. Are there any solutions that would involve leaving the references, classes, and modules as they stand?
Thank you.
The workaround is to have a ReferenceProperty in at least one of the models that doesn't restrict itself to a particular class, and then enforce only referencing that class in your own code.
e.g.,
class A(db.Model):
b = db.ReferenceProperty()
class B(db.Model):
a = db.ReferenceProperty(A)
You'll be able to assign any model instance to the b variable; just make sure you only assign actual Bs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With