Use union operator for queryset | to take union of two queryset. If both queryset belongs to same model / single model than it is possible to combine querysets by using union operator. One other way to achieve combine operation between two queryset is to use itertools chain function.
A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.
queryset acts just like a list so you can . pop() items and del() them.
You can have an empty queryset by doing this:
MyModel.objects.none()
Although i don't know how are you going to use that form, you can put that as your field's queryset in order to get what you need...
You can find more information here
@radtek's comment should be an answer as it is useful in similar scenarios but with different approach than the accepted answer.
If your queryset changes with the url in your view.
I am extending the answer with example as I used:
def my_view(request):
...
form = YourForm(initial={'field1':value1, 'field2':value2})
form.fields['field3'].queryset = YourModel.objects.filter('foo'=bar)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With