philipn/django-rest-framework-filters

RelatedMultipleFilter doesn't work as expected

fearsd opened this issue · 1 comments

I have 3 models (some fields are emitted to shorten the issue):

class Home(models.Model):
    address = models.CharField(max_length=150)
    formal_address = models.CharField(max_length=250, null=True, blank=True)
    homestead_number = models.CharField(null=True, blank=True, max_length=255)
    coords = models.JSONField(null=True, blank=True)
    county_type = models.CharField(max_length=150)

class UserAccount(AbstractBaseUser, PermissionsMixin):
    ...
    homes = models.ManyToManyField(Home, related_name='users')
    ...

class PersonalAccount(models.Model):
    user = models.ForeignKey(UserAccount, on_delete=models.CASCADE, related_name='personal_accounts')
    ...

And filters for them (here you can see fields you didn't see before in my models, don't mind):

class HomeFilter(filters.FilterSet):
    class Meta:
        model = Home
        fields = {
            'homestead_number': ['exact', 'icontains'],
            'county_type': ['exact', 'icontains']
        }

class UserFilter(filters.FilterSet):
    homes = filters.RelatedMultipleFilter(HomeFilter, queryset=Home.objects.all())
    class Meta:
        model = UserAccount
        fields = ('full_name', 'user_role')

class PersonalAccountFilter(filters.FilterSet):
    user = filters.RelatedFilter(UserFilter, queryset=UserAccount.objects.all())
    class Meta:
        model = PersonalAccount
        fields = ['account']

So I am trying to filter the personal accounts with /personal_accounts/?user__homes__homestead_number__icontains=1, but it seems that filtering doesn't work and returns all entries
image

I suggested that django's in-built method .filter() works like on the picture above, but it works as I expect:
Screen Shot 2021-04-14 at 11 20 59

Am I doing everything correctly or library has this bug? Also, thanks for your package, it saved my time! :)

Hi !! I am facing to the exact same issue !

All work well with the django's in-built method.

@fearsd, Have you find a workaround to this issue (since you have posted this issue) ?