Separate Class Files
See original GitHub issueSorry if this is a basic question with an obvious answer, I am relatively new to Python (long-time software engineer, just not with Python) and just attempting to use PonyORM.
I am building an application with quite a number of tables, relationships, and methods associated with the ORM classes. Due to this the model.py file I’m using, which started out just generated from the online editor, is getting rather large. My desire would be to separate each class into a separate class file under a “model” subdirectory within the project structure. However, since the relationships within the classes are dependent on one another this seems to setup a circular dependency which generates errors.
Is there any way to effectively separate out each class into a separate file given PonyORM relationships? I’ve put a very basic example below, not what I’m actually working on but easier to reference here, in this case having 4 separate class files (person.py, employee.py, contractor.py, and place.py) and potentially a master model.py file to load in the rest.
Any suggestions would be appreciated!
`from pony.orm import *
db = Database()
class Person(db.Entity): id = PrimaryKey(int, auto=True) name = Optional(str) email = Optional(str) phone = Optional(str) employee = Optional(‘Employee’) contractor = Optional(‘Contractor’)
class Employee(db.Entity): id = PrimaryKey(int, auto=True) employee_number = Optional(str) location = Optional(str) person = Required(Person) places = Set(‘Place’)
class Contractor(db.Entity): id = PrimaryKey(int, auto=True) contract_date = Optional(str) person = Required(Person)
class Place(db.Entity): id = PrimaryKey(int, auto=True) address = Optional(str) employees = Set(Employee)
db.generate_mapping()`
Thanks,
Chris
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Thanks @stephancill I’m copying it here for the record.
You can use the following project structure:
settings.py
is a file with the database settings:main.py
is a file with you application code. You putdb.generate_mapping
here:Note that it is not necessary to implicitly import all models, as they are accessible as attributes of the
db
object (e.g.db.User
)You can put the
db
object tobase.py
(orgeneral.py
), where you define your core models:Note that in the
User
model I can refer to theOrder
model defined in another module. You can also write it asUnfortunately, IDEs like PyCharm at this moment cannot recognize that
db.Order
refers to the specificOrder
class. You can import and use theOrder
class directly, but in some cases it will lead to a cyclic imports problem.In other model files you can
import db
from.base
:In the
/models/__init__.py
file you import all modules to ensure that all models classes are defined before thegenerate_mapping
method is called:And then you can write
And access models as
db
attributes likedb.User
,db.Order
, etc.If you want to refer models directly in your code instead of accessing them as the
db
attributes, you can import all models in__init__.py
:And then you can import model classes this way:
Mind posting a solution? I’m relatively new to Pony ORM and have come across a similar issue. In the past I would have something like this all in one file:
This is the part I’m trying to fix/ do better/ move to its own file: The more tables I have, the more bloated my database class becomes but so far I haven’t found
a goodany way to separate the entities from the DBConnector class.The rest of the class is your usual
Thanks!