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)
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:
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.
Example Model
Originally posted by @William-Wildridge in #48 (comment)