1

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.

inava
  • 19
  • 8

1 Answers1

0

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)
aminrd
  • 4,300
  • 4
  • 23
  • 45
  • Hey @aminrd, this works but I need the id to be excluded to be taken as a user input from the html. How will I do that? I can manually do id=5 and it works but I need it to be something like ```exclude(id=userinputforid)``` so I don't know how to get that user input here. – inava Nov 05 '19 at 00:57
  • You can capture that `id` from user in a url like `path('myfilter//', views.someFunction, name='my_filter')`? – aminrd Nov 05 '19 at 01:00
  • Weirdly enough, I didn't have to write anything in my query to exclude the user input id. In my filters.py Filter, I included exclude for the id and it works! Thanks for the help though! Do you happen to know anything about splicing/pagination in Django? – inava Nov 06 '19 at 17:04