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

Add custom filter field which is not model field #1536

Closed
0xSullivan opened this issue Oct 2, 2022 · 3 comments
Closed

Add custom filter field which is not model field #1536

0xSullivan opened this issue Oct 2, 2022 · 3 comments

Comments

@0xSullivan
Copy link

Hey
I was using an old version of django-filter and I added some non-model fields for filtering. for example for user I added a name filter with custom method which concats first_name and last_name and filter both of them but there is no name field in the User model.
After I updated to the latest version I faced this error:

TypeError: 'Meta.fields' must not contain non-model field names: name

I checked the codes and I found out this error happen here. in the comment it say warn if the field doesn't exist. but it actually raise a TypeError in line 349

I think it's better to make it a REAL warning instead a ERROR

@carltongibson
Copy link
Owner

Can you provide a minimal reproduce please, as it's not possible to see what's going on from the information given so far.

@gone
Copy link

gone commented Dec 21, 2022

A sample filter/model that replicates this:

class Profile(models.Model):
    location = models.CharField(_("location"), max_length=30)
    organization = models.CharField(_("organization"), max_length=30)
    title = models.CharField(_("title"), max_length=30)


class ProfileFilter(django_filters.FilterSet):
    search = django_filters.CharFilter(label="search", method="search")

    def search(self, queryset, name, value):
        return queryset

    class Meta:
        model = Profile
        fields = ("location", "organization", "title", "search")

The intent here would be that search isn't attached directly to the model, but appears in the fields list.

@gone
Copy link

gone commented Dec 21, 2022

Ug duplicate name error. Just name your filter something different than your method.

@carltongibson carltongibson closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2023
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

3 participants