Skip to content

Filter an existing django queryset #168

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

Merged
merged 3 commits into from
May 17, 2023

Conversation

andriilahuta
Copy link
Contributor

Slight modification of to_queryset method to allow filtering of existing django querysets.

@safwanrahman
Copy link
Collaborator

Can you please rebase it upon master and also describe the use case of this?

@andriilahuta
Copy link
Contributor Author

@safwanrahman it can be used when implementing a custom QuerySet class or searching otherwise pre-filtered queryset.

"""

qs = _get_queryset(klass)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not comfortable to use a private method _get_queryset as it is not documented anywhere.

I think it is better to use self._model._default_manager for getting the default manager as the documentation recommend it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@safwanrahman
Copy link
Collaborator

@laevilgenius Sorry for the too late reply. I have one issue with the PR. Are you able to fix it?

@@ -13,12 +13,17 @@ def _clone(self):
s._model = self._model
return s

def to_queryset(self, keep_order=True):
def filter_queryset(self, queryset, keep_search_order=True):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a internal function so it should start with _

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually meant it to be public, so I could do something like this:

class SearchQuerySet(QuerySet):
    def from_elastic(self, search):
        return search.filter_queryset(self)

Could you explain your reasoning?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I did not think about it before. Let me think about it so I can suggest a better approach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to_queryset should accept a queryset as optional parameter. So if queryset is passed, it will use that queryset. otherwise call the get_queryset method to get the queryset

@laevilgenius what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep to_queryset backwards compatible? I'm thinking def to_queryset(self, qs=None, keep_order=True) vs def to_queryset(self, keep_order=True, qs=None).

@safwanrahman
Copy link
Collaborator

I think there should be a get_queryset method like django rest framework, which will be called by the to_queryset method. Sot the filtering and others can be done by overwriting get_queryset method.

Can you add a documentation about that also?

@@ -13,12 +13,17 @@ def _clone(self):
s._model = self._model
return s

def to_queryset(self, keep_order=True):
def filter_queryset(self, queryset, keep_search_order=True):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. As we already have get_queryset method, you can make the filter there. So we do not need another method for filter_queryset


return queryset

def _get_queryset(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make this api public!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it private so it would be less confusing with IDE hinting for example, as we have public methods that are mostly related to filtering and search object manipulation. What would be the public usage of get_queryset?

@safwanrahman safwanrahman merged commit f6fa886 into django-es:master May 17, 2023
@safwanrahman
Copy link
Collaborator

I think we can merge this PR as it is now. Sorry for the late in reviewing, better late than never! Thanks @andriilahuta

@andriilahuta andriilahuta deleted the filter_queryset branch May 17, 2023 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants