Skip to content
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

Filters don't apply to reverse foreign keys. #97

Open
William-Wildridge opened this issue Jan 26, 2023 · 0 comments
Open

Filters don't apply to reverse foreign keys. #97

William-Wildridge opened this issue Jan 26, 2023 · 0 comments

Comments

@William-Wildridge
Copy link

The get_nested_field() function by @pablodiazgtierrez is a really nice addition to this library, but doesn't seem to work for reverse relationships.

I have the same use case as Pablo (needing to filter by company in a multitennant SAAS). However in my use case the object relationship direction of the object is a reverse one, (Example Model below).

I'm using this line in get_extra_filter_kwargs:

def get_extra_filter_kwargs(request, *args, **kwargs):
            return {
                'companies__company': request.user.memberships.first().company,
            }

Similar to Pablo's original issue, for requests to /Users?filter=userName+eq+"example_username" the result is blank, but /Users/1 and Users?attributes=userName do work correctly, and apply the company restrictions as expected.

I think I've diagnosed that the hasattr() line (shown below) that was added in this merge is causing this the filtering to stop prematurely.

            if not hasattr(obj, field_name):
                return None

Example Model

class UserInCompany(models.Model):
        company = models.ForeignKey(Company, related_name = 'company_members', on_delete=models.CASCADE,  null=True, blank=True)
	user = models.ForeignKey(User, on_delete=models.CASCADE, related_name = 'memberships', null=True, blank=True)
class Company(models.Model):
        company_name = models.CharField(max_length=200, null=True, blank=True)

Originally posted by @William-Wildridge in #48 (comment)

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

No branches or pull requests

1 participant