ekmungai / python-accounting

Python Double Entry Accounting with a focus on IFRS Compliant Reporting
MIT License
83 stars 13 forks source link

Get a list of accounts #9

Closed heysarver closed 1 month ago

heysarver commented 1 month ago

What is the proper way to load an existing entity into a new session and get a list of accounts?

I'm currently trying this but am getting an error:

AttributeError: 'AccountingSession' object has no attribute 'entity'

with get_session(engine) as session:
    print(f"Session: {session}")
    entity = session.query(Entity).filter_by(name=entity_name).first()

    if entity:
        entity_id = entity.id
        accounts = session.query(Account).filter(Account.entity_id == entity_id).all()
        print(f"Accounts: {accounts}")
ekmungai commented 1 month ago

Hey @heysarver,

Did you already initialise an entity and add it to the session?


from python_accounting.database.session import get_session
from python_accounting.models import Entity, Currency

with get_session(engine) as session:
    entity = Entity(name="Example Company")
    session.add(entity)
    session.commit() # This automatically sets up a Reporting Period for the Entity

    currency = Currency(name="US Dollars", code="USD", entity_id=entity.id)
    session.add(currency)
    session.commit()

Also, the entity filter accounts = session.query(Account).filter(Account.entity_id == entity_id).all() is unnessesary because all accounting objects are filtered automatically by the current session's entity id. See isolation tests

heysarver commented 1 month ago

Thanks, I got it working after reviewing the tests. I forgot to set session.entity = entity.