Skip to content

fetch_related() should allow a manager to be passed as an arg. #2234

Description

@markmiscavage

Is your feature request related to a problem? Please describe.
I have a model where a FK related object does not show in queries because the related object isn't in the query results from the default model manager. It would be cleaner and faster to be able to pass in a manager or some other argument to allow fetch_related to return that object.

example:
class GroupOneManager(Manager):
def get_queryset(self):
return (
super()
.get_queryset()
.filter(group='One')
)

class User(BaseModel):
id = fields.CharField(pk=True, max_length=255)
group = fields.CharField(max_length=255)
all_objects = Manager()

class meta: 
    manager = GroupOneManager()

class Controls(BaseModel):
user = fields.ForeignKeyField(User)

user = await User.get(group='Two').first()
await Controls(user=user).save()
control = await Controls.all.first()
await control.fetch_related('user')
print(control.user) # Will be None because user is in group='Two' which isn't part of default manager

Describe the solution you'd like
await object.fetch_related('user', manager=GroupModelManager)
of
await object.fetch_related('user', exclude_default_manger=True)

Describe alternatives you've considered
The alternative is to do a separate lookup of the object which isn't as clean or pythonic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions