[Answered ]-Django: Limit QuerySet to user input (checkboxes)

1πŸ‘

βœ…

I wonder if the data model here couldn’t use some tweaking? You might want to have a supply table with twenty rows and an intermediate table connecting them (that is a ManytoMany(Supply) or something like that). Then you could just have a multi select field, rather than 20 check boxes (unless you really need them for some other reason).

If you need to add another supply, it’s simply adding another row, rather than a schema migration.

πŸ‘€James R

1πŸ‘

supplies = Supply.objects.filter( supply1 = 1 )

And if you want to filter again:

supplies = supplies.filter(supply2 = 1) 

The filter() method returns a QuerySet, so you can chain as many filter() calls as you like.

Leave a comment