You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Would love to get your thoughts on the best way to handle accessing nested foreign key relationships. Here's a made up example (where the relationship is Book -> Author -> PublishingCompany).
Now let's say we have another Domain, Purchasing that's responsible for placing orders. From this domain, say we want to do something access some info via book.author.publishing_company. What would be the best way to handle this?
I can see two options, but neither seem great.
Option 1: Inside BookService.get_book(), we can add Book.objects.get(id=id).select_related('author__publishing_company'). The downside of this is that we are always fetching extra data, whether the caller uses it or not.
Option 2: Inside the Purchasing domain, we issue separate calls to BookAPI, PublishingCompanyAPI, etc but this is also doing extra work.
Ideally, there would be some way of cleanly passing to BookAPI which data should be fetched.
The text was updated successfully, but these errors were encountered:
Did you know you can build up query sets? They are lazily evaluated when you access some data. So you could optionally do the .select_related if a parameter is passed, for example. The API response will change shape though, so keeping an empty response in place would be a good idea, such as null values, or an empty object.
Would love to get your thoughts on the best way to handle accessing nested foreign key relationships. Here's a made up example (where the relationship is
Book
->Author
->PublishingCompany
).Domain
Inventory
:Now let's say we have another Domain,
Purchasing
that's responsible for placing orders. From this domain, say we want to do something access some info viabook.author.publishing_company
. What would be the best way to handle this?I can see two options, but neither seem great.
Option 1: Inside
BookService.get_book()
, we can addBook.objects.get(id=id).select_related('author__publishing_company')
. The downside of this is that we are always fetching extra data, whether the caller uses it or not.Option 2: Inside the
Purchasing
domain, we issue separate calls toBookAPI
,PublishingCompanyAPI
, etc but this is also doing extra work.Ideally, there would be some way of cleanly passing to
BookAPI
which data should be fetched.The text was updated successfully, but these errors were encountered: