Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: When To Use QuerySet None

Tags:

python

django

Just came across this in the django docs

Calling none() will create a queryset that never returns any objects and no query will be executed when accessing the results. A qs.none() queryset is an instance of EmptyQuerySet.

I build a lot of CRUD apps (surprise) and I can't think of a situation where I would need to use none().

Why would one want to return an EmptyQuerySet?

like image 372
super9 Avatar asked Jan 12 '13 03:01

super9


People also ask

How do I check if a field is empty in Django?

Unless you set null=True as well, your database should complain if you tried to enter a blank value. If your foreign key field can take null values, it will return None on null, so to check if it was "left blank", you could simply check if the field is None .

What is QuerySet in Django?

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.

Is __ null Django?

In Django queryset we have __isnull to check if a value is null. Here were filtering all the values from MyModel model, which have an alias = Null. exclude allows you to pass multiple arguments, it will the then check for every argument. If your query is still more complex, you can use Django's Q Objects.


2 Answers

Usually in instances where you need to provide a QuerySet, but there isn't one to provide - such as calling a method or to give to a template.

The advantage is if you know there is going to be no result (or don't want a result) and you still need one, none() will not hit the database.

For a non-realistic example, say you have an API where you can query your permissions. If the account hasn't been confirmed, since you already have the Account object and you can see that account.is_activated is False, you could skip checking the database for permissions by just using Permission.objects.none()

like image 160
DanielB Avatar answered Oct 19 '22 06:10

DanielB


In cases where you want to append to querysets but want an empty one to begin with Similar to conditions where we instantiate an empty list to begin with but gradually keep appending meaningful values to it example..

def get_me_queryset(conditionA, conditionB, conditionC):     queryset = Model.objects.none()      if conditionA:         queryset |= some_complex_computation(conditionA)     elif conditionB:         queryset |= some_complex_computation(conditionB)      if conditionC:         queryset |= some_simple_computation(conditionC)      return queryset 

get_me_queryset should almost always return instance of django.db.models.query.QuerySet (because good programming) and not None or [], or else it will introduce headaches later..

This way even if none of the conditions come True, your code will still remain intact. No more type checking

For those who do not undestand | operator's usage here:

queryset |= queryset2 

It translates to:

queryset = queryset + queryset 
like image 25
Arpit Singh Avatar answered Oct 19 '22 07:10

Arpit Singh