Search code examples
pythonhtmldjangouser-inputdjango-filter

How to use Django filter's exclude argument with user input?


I'm trying to filter my query results in Django according to user input. I have my filter query working fine with something like Arecords.objects.select_related('b_id').filter(id=5)

This works just fine. But what I ideally need is that the user inputs a value in the browser for "id" and I want to exclude those from the result. How would I do something like that?

Is there a way I can just use an html form input in my filter() query in Django? Or can I use something else?

I've tried using django_filters. It works for the other fields where I'm returning results that contain the user input etc, but I don't know how to deal with a "not in" or "not equal to"

What I want is that user enters their own id and I want the query to be filtered so that it excludes the fields with that id.


Solution

  • exclude(**kwargs) returns a new QuerySet containing objects that do not match the given lookup parameters. For example, you can try:

    Arecords.objects.select_related('b_id').exclude(id=5)