SUBJECTS
|
BROWSE
|
CAREER CENTER
|
POPULAR
|
JOIN
|
LOGIN
Business Skills
|
Soft Skills
|
Basic Literacy
|
Certifications
About
|
Help
|
Privacy
|
Terms
|
Email
Search
Test your basic knowledge |
Django Queryset
Start Test
Study First
Subject
:
it-skills
Instructions:
Answer 50 questions in 15 minutes.
If you are not ready to take this test, you can
study here
.
Match each statement with the correct term.
Don't refresh. All questions and answers are randomly picked and ordered every time you load a test.
This is a study tool. The 3 wrong answers for each question are randomly chosen from answers to other questions. So, you might find at times the answers obvious, but you will see it re-enforces your understanding as you take the test each time.
1. Sometimes - the Django query syntax by itself can't easily express a complex WHERE clause. For these edge cases - Django provides this QuerySet modifier -- a hook for injecting specific clauses into the SQL generated by a QuerySet.
extra(select=None - where=None - params=None - tables=None - order_by=None - select_params=None)
annotate(args - *kwargs)
ForeignKey
clear() method
2. Lookup type that returns results greater than a given value.
Multi-table inheritance
Q(question__istartswith='What')
Meta class
gt
3. The value given in the absence of a specified value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
add()
range
QuerySet
default
4. Lookup type that takes either True or False and corresponds to SQL queries of IS NULL and IS NOT NULL - respectively.
exists()
startswith
Entry.objects.filter(pub_date__range=(start_date - end_date))
isnull
5. Accomplish this by using the field name of related fields across models - separated by double underscores - until you get to the field you want. For example - to get all Entry objects with a Blog whose name is 'Beatles Blog': Entry.objects.filter(blo
&
relationship spanning
filter(**kwargs)
Restrictions on field names
6. A manager method which returns a single object. If there are no results that match the query - this method will raise a DoesNotExist exception. If more than one item matches this query - the method will raise MultipleObjectsReturned.
get(**kwargs)
aggregate(args - *kwargs)
lt
reverse()
7. Use this method to reverse the order in which a queryset's elements are returned. Calling this method a second time restores the ordering back to the normal direction.
unique
reverse()
iexact
Q(question__istartswith='What')
8. Lookup type that returns results with a case-sensitive end sequence.
values(*fields)
endswith
default
iexact
9. The default for this is the name of the child class followed by '_set'.
extra(select=None - where=None - params=None - tables=None - order_by=None - select_params=None)
Manager
reverse name
aggregate(args - *kwargs)
10. Lookup type that returns results in a given list.
in
class attributes
choices
startswith
11. Lookup type that returns results less than or equal to a given value.
lte
Entry.objects.filter(pub_date__year=2005).delete()
values(*fields)
Meta class
12. Returns a new QuerySet containing objects that match the given lookup parameters.
==
clear() method
distinct()
filter(**kwargs)
13. This method returns tuples of values when iterated over. Each tuple contains the value from the respective field passed into the call to this method -- so the first item is the first field - etc.
Custom model methods
regex
choices tuple
values_list(*fields)
14. This model method saves a model instance to the database. This method has no return value.
year
Entry.objects.filter(id__in=[1 - 3 - 4])
many-to-many relationship
save()
15. Performs an SQL update query for the specified fields - and returns the number of rows affected. This method is applied instantly and the only restriction on the QuerySet that is updated is that it can only update columns in the model's main table. F
get(**kwargs)
update(**kwargs)
Manager methods
Many-to-many relationship through an intermediate model
16. A convenience method for looking up an object with the given kwargs - creating one if necessary.
regex
intermediate models
get_or_create(**kwargs)
db
17. This query finds all entries between a start date of start_date and an end date of end_date.
Entry.objects.filter(pub_date__range=(start_date - end_date))
in_bulk(id_list)
one-to-one relationship
get_or_create(**kwargs)
18. This class type is useful when you just want to use the parent class to hold information that you don't want to have to type out for each child model. This class isn't going to ever be used in isolation. When it is used as a base class for other mode
class type
annotate(args - *kwargs)
evaluation
Abstract base class
19. If True - this field is the primary key for the model.
save()
Restrictions on field names
choices tuple
primary_key
20. This gives your model metadata.
using(alias)
Meta class
aggregate(args - *kwargs)
create(**kwargs)
21. Evaluates the QuerySet (by performing the query) and returns an iterator over the results. A QuerySet typically caches its results internally so that repeated evaluations do not result in additional queries; this method will instead read results dire
primary_key
iterator()
gte
class attributes
22. This query finds all entries with an id greater than 4.
Entry.objects.filter(id__gt=4)
Abstract base class
startswith
in_bulk(id_list)
23. A Python "magic method" that returns a unicode "representation" of any object.
in
QuerySet
__unicode__()
evaluation
24. Can be used to remove all many-to-many relationships for an instance
clear() method
filter(**kwargs)
F()
latest(field_name=None)
25. Lookup type that returns results with a case-sensitive start sequence.
startswith
get(**kwargs)
evaluation
Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')
26. Defines a many-to-one relationship. ou use it just like any other Field type: by including it as a class attribute of your model.
ForeignKey
Q(question__istartswith='What')
Manager
delete()
27. Lookup type that returns results with a case-insensitive end sequence.
iendswith
count()
select_related()
MultipleObjectsReturned
28. Defined by a ManyToManyField. You use it just like any other Field type: by including it as a class attribute of your model.
MultipleObjectsReturned
many-to-many relationship
create(**kwargs)
|
29. If this option is True - the field is allowed to be blank. Default is False.
help_text
filter(**kwargs)
blank
startswith
30. In this case - an intermediate model can have multiple foreign keys to the source model. Here - two foreign keys to the same model are permitted - but they will be treated as the two (different) sides of the many-to-many relation.
Many-to-many relationship through an intermediate model
year
Many-to-many relationship to self
evaluation
31. This represents a collection of objects from your database. It can have zero - one or many filters.
all()
QuerySet
save()
ForeignKey
32. Defined by a OneToOneField. You use it just like any other Field type: by including it as a class attribute of your model.
one-to-one relationship
in
MultipleObjectsReturned
Meta class
33. Returns an EmptyQuerySet -- a QuerySet that always evaluates to an empty list. This can be used in cases where you know that you should return an empty result set and your caller is expecting a QuerySet object (instead of returning an empty list - fo
none()
Entry.objects.all().update(n_pingbacks=F('n_pingbacks') + 1)
Manager
|
34. An iterable (e.g. - a list or tuple) of 2-tuples to use as options for this field. If this is given - Django's admin will use a select box instead of the standard text field and will limit options to those given.
Multi-table inheritance
istartswith
choices
Abstract base class
35. This query deletes all Entry objects with a pub_date year of 2005.
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
values(*fields)
Entry.objects.filter(pub_date__year=2005).delete()
gte
36. This is a criterion that narrow down a QuerySet based on given parameters.
Field lookups
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
filter
db
37. A manager method that returns a new QuerySet containing objects that do not match the given lookup parameters.
exclude(**kwargs)
blank
through argument
all()
38. Exception raised by get(**kwargs) if more than one item matches the query.
__unicode__()
DoesNotExist
Many-to-many relationship through an intermediate model
MultipleObjectsReturned
39. This sets a field to a particular value for all the objects in a QuerySet. You can only set non-relation fields and ForeignKey fields using this method.
all()
update()
extra(select=None - where=None - params=None - tables=None - order_by=None - select_params=None)
class attributes
40. This model method is used for updating a ManyToManyField.
iendswith
year
add()
gt
41. To activate your models
edit your settings file and change the INSTALLED_APPS setting to add the name of the module that contains your models.py.
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
exclude(**kwargs)
defer(*fields)
42. Returns a ValuesQuerySet -- a QuerySet that returns dictionaries when used as an iterable - rather than model-instance objects.
relationship spanning
values(*fields)
pickling
&
43. This method immediately deletes the object and has no return value.
delete()
choices tuple
extra(select=None - where=None - params=None - tables=None - order_by=None - select_params=None)
Entry.objects.all().update(n_pingbacks=F('n_pingbacks') + 1)
44. Exception raised by get(**kwargs) if no items match the query.
DoesNotExist
Entry.objects.filter(pub_date__range=(start_date - end_date))
choices
==
45. Each one of these is a Python class that subclasses django.db.models.Model. Each attribute of one of these represents a database field.
through argument
__unicode__()
related_name
model
46. a QuerySet can be sliced - using Python's array-slicing syntax.
unique
ordered
slicing
get_or_create(**kwargs)
47. This method is for controlling which database the QuerySet will be evaluated against if you are using more than one database. The only argument this method takes is the alias of a database - as defined in DATABASES.
Q()
aggregate(args - *kwargs)
blank
using(alias)
48. Conjuntion operator for Q objects.
QuerySet
Restrictions on field names
related_name
&
49. Returns the object matching the given lookup parameters
edit your settings file and change the INSTALLED_APPS setting to add the name of the module that contains your models.py.
get(**kwargs)
reverse name
Q(question__istartswith='What')
50. If you are using this attribute on a ForeignKey or ManyToManyField - you must always specify a unique reverse name for the field.
many-to-one relationship
iregex
related_name
using(alias)