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.

Can't I use .values() with relationships?

See original GitHub issue

Take the following example :

class Task(Model):
  title = fields.CharField(255)

class Actions(Model):
  title = fields.CharField(255)
  task = fields.ManyToManyField('models.Task', related_name='actions')

async def main():
  tasks = await Task.all().prefetch_related('actions').values('actions')

I’m trying to achieve the following structure:

[
    {
        "id" : 1,
        "title" : "Task 1",
        "actions" : [
            {
                "id" : 1,
                "title" : "Action 1"
            }
        ]
    }
]

But it gives me Selecting relation "actions" is not possible, select concrete field on related model

Is this intended? Do I need to use a serializer to be able to achieve this structure?

How would I choose to return only specific fields from actions if not using values?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
grigicommented, Sep 30, 2019

Could we not make it use kwargs? E.g. values(‘name’, childs=Childs.all().values(…)) ? And if you pass a queryset it would auto prefetch?

This would implement a useable deserialisation step 🤔 And be performant

(Generally in favour)

1reaction
abondarcommented, Sep 29, 2019

I have idea that it would be quite neat to have such api

ojb.values('name', Prefetch("childs", queryset=Child.all().values('id', 'name')))

That would result in such dict

[{
  'name': 'Res 1',
  'childs': [{
    'id': 1,
    'name': 'John'
  }]
}]

@grigi What do you think about such design?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Values in a Relationship: 8 Core Values for Relationships - 2022
1. Communication: Keeping open lines of communication with your partner can set you up to having a successful relationship. · 2. Equality: Seeing ......
Read more >
10 Core Values of a Lasting Relationship - Lifehack
1. Trust ... This core value stands above all others. It is the foundation of your relationship. Without trust you basically have nothing....
Read more >
Use Relationship column value as property - laravel
I have a one to many relation. They all have a unique column ( besides ID ). After running the query, Im trying...
Read more >
Value-based Conditional Relationships - Laracasts
After looking at your table setup, you should be using a belongsTo() method to describe the relationship. The second argument should be the...
Read more >
11 Core Relationship Values Every Couple Must Have
For example, marriage core values, such as respect and forgiveness, when shared among partners, can be of immense help in conflict management.
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