question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide an API for only fetching certain database fields

See original GitHub issue

Consider 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:closed
  • Created 4 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
grigicommented, Apr 11, 2020

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.

2reactions
grigicommented, Apr 9, 2020

Ok, let’s just do this. I could use it for the pydantic serialisation to fetch less data as well.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found