Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Optimize M2M Field #9276

Closed
jonasN5 opened this issue Mar 7, 2024 · 2 comments
Closed

Optimize M2M Field #9276

jonasN5 opened this issue Mar 7, 2024 · 2 comments

Comments

@jonasN5
Copy link

jonasN5 commented Mar 7, 2024

This is linked to #4917

return relationship.all() if hasattr(relationship, 'all') else relationship

Why can't we just allow some customization at this line? Usually with any toolbox, we provide a best effort default and give the user the option to customize. In this case, the relations will ALWAYS be fetched with .all(). I have a simple case where I need an annotation on each child and so a single query is enough, if done on the ManyRelatedField level. How about making the ManyRelatedField also accept a queryset argument and default to .all() if there is no queryset argument?

@auvipy
Copy link
Member

auvipy commented Mar 7, 2024

good point, can you share some PoC?

@jonasN5
Copy link
Author

jonasN5 commented Mar 7, 2024

class ManyRelatedField(Field):
    ...
    queryset = None

    def __init__(self, child_relation=None, *args, **kwargs):
        self.queryset = kwargs.pop('queryset', self.queryset)
        ...

      

    def get_attribute(self, instance):
        # If a custom queryset has been set, use that before falling back to the best effort
        if self.queryset:
            return self.queryset
        ...

Super simple

Or explicit parameter

    def __init__(self, child_relation=None, queryset=None, *args, **kwargs):
        self.queryset = queryset

@encode encode locked and limited conversation to collaborators Mar 19, 2024
@tomchristie tomchristie converted this issue into discussion #9312 Mar 19, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants