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.

how to filter if filter as OR across two or more fields for same model?

See original GitHub issue

Issue Description

Let’s say I want to run a filter like this

User.objects.filter(Q(username="admin") | Q(first_name="test"))

I understand drest does not support this in the end point.

How can I modify so as to allow something like this in the endpoint? I am okay to make a PR for this. I just want to get some guidance so I can conform to drest architecture

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ryochijicommented, Nov 6, 2018

I think this is tricky to do at the framework level, since we’ll need to update the query syntax to include grouping (e.g. to support queries like (A | B) & C). My suggestion is to override ViewSet.filter_queryset() and use custom query params:

class UserViewSet(...):
    def filter_queryset(self, queryset):
        queryset = super(FooViewSet, self).filter_queryset(queryset)
        admin = self.request.GET.get('admin')
        first_name = self.request.GET.get('first_name')
        if admin and first_name:
            queryset.filter(Q(admin=admin) | Q(first_name=first_name))
        return queryset
0reactions
denny64commented, Dec 9, 2018

@ryochiji Do you know if dynamic-rest will work with django-filters to solve this issue? I haven’t been able to get them to play nicely together

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filter by using advanced criteria - Microsoft Support
If the data you want to filter requires complex criteria (such as Type = "Produce" OR Salesperson = "Davolio"), you can use the...
Read more >
Excel FILTER function with formula examples - Ablebits
Tip. To filter duplicates based on the values in the key columns, include only those specific columns in the COUNTIFS function.
Read more >
How to apply multiple filtering criteria by combining AND and ...
Choose a built-in filter. Enter the benchmark amount. As you can see in Figure D, James has only one commission that falls below...
Read more >
How to filter multiple values in only one column in Excel?
Select Filter the list, in-place option from the Action section; (2.) Then, select the data range that you want to filter in the...
Read more >
How do I apply one filter to multiple fields? - Looker Community
Say you want to have a single filter apply to two separate fields (fields A and B). The end goal is to be...
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