QuerySet

QuerySet

QuerySet

and()

__getstate__()

QuerySet

QuerySet

__dict__

import pickle

pickle . dumps(query_set_obj)



__repr__()

QuerySet

list()

REPR_OUTPUT_SIZE

repr()

print query_set_obj



QuerySet

len()

__len__()

QuerySet

QuerySet

QuerySet

__len__()

len()

print len (query_set_obj)



__iter__()

QuerySet

QuerySet

QuerySet

QuerySet

iter()

__iter__()

for row in query_set_obj:

print row



__nonzero__()

QuerySet

bool()

QuerySet

__nonzero__()

if query_set_obj:

print "NOT EMPTY"



QuerySet

__and__()

__or__()

QuerySet

print query_set_obj1 and query_set_obj2

print query_set_obj1 or query_set_obj2



Theclass defined in the Django Python web application framework is used to manage query results returned from the database. From the developer perspective,instances provide a high-level interface for dealing with query results. Part of the reason for this is that instances of this class behave very similarly to other Python primitive types such as lists. This is accomplished by defining "magic" methods, or, operator overloading. If an operator for a specific instance is overloaded, it simply means that the default behavior for that operator has been replaced when the operand involves this instance. This means that we can do things such as use twoinstances in an and expression that will invoke custom behavior rather than the default behavior. This useful because it is generally easier and more intuitive to use operators in certain contexts than invoking methods. In the case of using two instances in an and expression, it makes more sense to use the built-in and Python keyword than it does to invoke somemethod on the instance. The Django provides several "magic" methods that do just this and we will discuss some of them below. Python instances that need to provide custom pickling behavior need to implement themethod. Theclass provides an implementation of this method. The Django implementation removes any references to self by copying theattribute. Here is an example of how this method might get invoked.The representation of Python instances is provided by themethod if it is defined. The Django implementation will call the builtinfunction on itself. The size of the resulting list is based on thevariable which defaults to 20. The result of calling the builtinfunction on this new list is then returned. Here is an example of how this method might get invoked.The length of theinstance can be obtained by calling the builtinfunction while using the instance as the parameter. In order for this to work, amethod must be defined by the instance. In the case of Django instances, the first thing checked is the length of the result cache. The result cache is simply a Python list that exists in Python memory to try and save on database query costs. However, if the result cache is empty, it is filled by theiterator. If the result cache is not empty, it is extended by theiterator. This means that the result cache is updated with any results that should be in the result cache but are not at the time of theinvocation. The builtinfunction is then called on the result cache and returned. Here is an example of how this method might get invoked. Python allows user-defined instance to participate in iterations. In order to do so, the class must define anmethod. This method defines the behavior for how individual elements in a set are returned in an iteration. Theimplementation of this method will first check if the result cache exists. If theresult cache is empty, the iterator defined for theinstance is returned. The iterator does the actual SQL querying and so in this scenario, the iteration looses out on the performance gained by having cached results. However, if a result cache does exist in theinstance, the builtinfunction is called on the cache and this new iterator is returned. Here is an example of how themethod might be invoked. Python instances can also take part in if statements and invoke custom behavior defined by themethod. If an instance defines this method, it will be invoked if the instance is an operand in a truth test. The Django implementation of this method first checks for a result cache, as does most of the other "magic" methods. If the result cache does exist, it will return the result of calling the builtinfunction on the result cache. If there is no result cache yet, the method will attempt to retrieve the first item by performing an iteration on theinstance. If the first item cannot be found, false is returned. Here is an example of how themight be invoked.Finally, Python instance may be part or and an or Python expressions. The Django instance defines bothandmethods. When these methods are invoked, they will change the underlying SQL query used by returning a newinstance. Here is an example of how both these methods may be used.