Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

need to call items to have them included ... #57

Open
papak69 opened this issue Aug 19, 2020 · 1 comment
Open

need to call items to have them included ... #57

papak69 opened this issue Aug 19, 2020 · 1 comment

Comments

@papak69
Copy link

papak69 commented Aug 19, 2020

Hi,

I found something interesting. My db model has a few foreign relationships ... and I found that in the crud operation I need to "call" them, otherwise they're not included ... any ideas why this would be?

model:
`class Invoice(Base):

__tablename__ = "invoice"

id = Column(Integer, primary_key=True)
net_total = Column(Float(), nullable=False, server_default="0")
tax = Column(Float(), nullable=False, server_default="0")
total = Column(Float(), nullable=False, server_default="0")
invoice_num = Column(Integer)
year = Column(Integer, nullable=False, server_default=str(now.year))
# STATUSES: 1:open, 2:paid
status = Column(Integer, nullable=False, server_default="1")
invoice_date = Column(Date(), server_default=str(now))
invoice_due = Column(Date(), server_default=str(now))
service_rendered = Column(Date(), server_default=str(now))
created = Column(DateTime(), server_default=str(now))
updated = Column(DateTime(), server_onupdate=str(now))
description = Column(Unicode(255), server_default=f"Zaračunavamo vam ... ")

# foreign keys & relationships
company_id = Column(Integer(), ForeignKey("company.id"), nullable=False)
company = relationship("Company", back_populates="invoices")
partner_id = Column(Integer(), ForeignKey("partner.id"), nullable=False)
partner = relationship("Partner", back_populates="invoices")
items = relationship("InvoiceItems", back_populates="invoice")`

crud:

`
def get_invoices(db: Session, page: int = 1, limit: int = 100):

query = db.query(models.Invoice)

filter_spec = [{'field': 'status', 'op': '==', 'value': 2}]

filtered_query = apply_filters(query, filter_spec)

sort_spec = [
    {'model': 'Invoice', 'field': 'total', 'direction': 'desc'},
]

sorted_query = apply_sort(query, sort_spec)

paginated_query, pagination = apply_pagination(
    sorted_query, page_number=1, page_size=3)

query_result = paginated_query.all()

# for whatever magic you need to call children to be included?
for item in query_result:
    # just call them ...
    company = item.company
    partner = item.partner
    items = item.items
    # ... and do nothing.
    pass

result = {"rows": pagination.total_results,
          "pages": pagination.num_pages, "items": query_result}

return result`
@papak69
Copy link
Author

papak69 commented Aug 19, 2020

I am talking about this part ...

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant