Provide an API for only fetching certain database fields
See original GitHub issueConsider the following:
class Employee(Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=1024)
... 100+ other fields
async def get_employee_names():
employees = await Employee.all()
return [employee.name for employee in employees]
In this case, get_employee_names
only needs a single field from the employee
table, which is name
. If Employee
has a lot of fields in the table (as often occurs with mature databases), we serialize all those fields into memory even when we only need a single field. That’s wasteful.
An improved API might be
async def get_employee_names():
employees = await Employee.only('name').all()
return [employee.name for employee in employees]
Django implements this API as documented here: https://docs.djangoproject.com/en/2.2/ref/models/querysets/#only
Issue Analytics
- State:
- Created 4 years ago
- Comments:16 (8 by maintainers)
Top Results From Across the Web
How to Fetch Data using API and SQL databases!
We are going to learn fetching data from an API and how you can use SQL databases to extract data and save it...
Read more >Is there a way to return only certain fields through an API url ...
Solved: Hello, I am trying to pull simple reports that go beyond Canvas' built-in reports- so I have been working with the API....
Read more >Fetching Data from the Database - Manning
I'll parse the JSON string from the API into a hash map, like we parsed a client request and then I'll use _.pick()...
Read more >How to Use an API: Just the Basics 2022 | TechnologyAdvice
Ever wonder what an API is or how to use one? APIs allow one program to request data from another. Learn more about...
Read more >How do I only pull certain data from an API field?
I didn't really look at the api, so might have to adjust the fetch code, but the slice() should work. Here's also the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a working implementation in #350 Just need to write docs for it as the interactions are quite complex, as I allow partial updates.
Please have a look.
Ok, let’s just do this. I could use it for the pydantic serialisation to fetch less data as well.