Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty queryset by default in django form fields

People also ask

How do I append Queryset in Django?

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.

What does Queryset []> mean?

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.

How do I remove an object from Queryset?

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)