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. This query uses an F object to increment the pingback count for every entry in the blog.
Warning
: Invalid argument supplied for foreach() in
/var/www/html/basicversity.com/show_quiz.php
on line
183
2. Takes the ouput of one filter and uses it as input for another filter. This works because a refinement of a QuerySet is itself a QuerySet.
Q()
contains
slicing
filter chaining
3. This method is more or less the opposite of defer(). You call it with the fields that should not be deferred when retrieving a model. If you have a model where almost all the fields need to be deferred - using this method to specify the complementary
Q(question__istartswith='What')
iexact
only(*fields)
update(**kwargs)
4. 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.
values_list(*fields)
in_bulk(id_list)
Proxy model
relationship spanning
5. This query updates all the headlines with pub_date in 2007 to read 'Everything is the same'.
Warning
: Invalid argument supplied for foreach() in
/var/www/html/basicversity.com/show_quiz.php
on line
183
6. 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.
isnull
Restrictions on field names
update()
primary_key
7. This object encapsulates a collection of keyword arguments - with the keys being field lookup types. These objects can be combined using the & and | operators - as well as negated with the ~ operator.
Q()
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
aggregate(args - *kwargs)
many-to-one relationship
8. These add custom "row-level" functionality to your objects. These act on a particular model instance.
intermediate models
Custom model methods
many-to-many relationship
|
9. Returns the object matching the given lookup parameters
get(**kwargs)
evaluation
lte
Custom model methods
10. Returns a DateQuerySet -- a QuerySet that evaluates to a list of datetime.datetime objects representing all available dates of a particular kind within the contents of the QuerySet.
Warning
: Invalid argument supplied for foreach() in
/var/www/html/basicversity.com/show_quiz.php
on line
183
11. By default - results returned by a QuerySet are ordered by the ordering tuple given by the ordering option in the model's Meta. You can override this on a per-QuerySet basis by using the this method.
order_by(*fields)
istartswith
startswith
only(*fields)
12. These are specified as keyword arguments to the QuerySet methods filter() - exclude() and get(). These take the form field__lookuptype=value .
using(alias)
Field lookups
~
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
13. Operator for comparing two model instances for equality. Behind the scenes - it compares the primary key values of two models.
count()
Entry.objects.filter(pub_date__range=(start_date - end_date))
get_absolute_url()
==
14. Returns a new QuerySet that uses SELECT DISTINCT in its SQL query. This eliminates duplicate rows from the query results.
reverse name
distinct()
Custom model methods
endswith
15. Negation operator for Q objects.
==
relationship spanning
~
blank
16. Defined by a OneToOneField. You use it just like any other Field type: by including it as a class attribute of your model.
get_absolute_url()
null
one-to-one relationship
reverse()
17. 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.
when you add new apps to INSTALLED_APPS
&
extra(select=None - where=None - params=None - tables=None - order_by=None - select_params=None)
count()
18. This model method saves a model instance to the database. This method has no return value.
save()
through argument
MultipleObjectsReturned
help_text
19. Exception raised by get(**kwargs) if more than one item matches the query.
filter
default
MultipleObjectsReturned
count()
20. Lookup type that returns results less than or equal to a given value.
annotate(args - *kwargs)
pk
count()
lte
21. Here - you can't use add - create - or assignment (i.e. - beatles.members = [...]) to create relationships. You need to specify all the detail for the relationship required by the intermediate model.
intermediate models
iendswith
ForeignKey
Many-to-many relationship through an intermediate model
22. Disjunction operator for Q objects.
|
contains
endswith
when you add new apps to INSTALLED_APPS
23. Evaluation happens upon use the "step" parameter of slice syntax - the first time you iterate over it - when pickling or caching results - upon calling repr() - upon calling len() - upon calling list() - upon calling bool()
Entry.objects.filter(pub_date__year=2005).delete()
evaluation
year
model
24. This model type is useful if you only want to modify the Python-level behavior of a model - without changing the models fields in any way. This creates a stand-in for the original model. You can create - delete and update instances of this new model
using(alias)
F()
Proxy model
search
25. A QuerySet is iterable - and it executes its database query the first time you iterate over it.
regex
exclude(**kwargs)
lt
iteration
26. If True - this field is the primary key for the model.
get(**kwargs)
primary_key
filter(**kwargs)
defer(*fields)
27. This query finds all entries with an id greater than 4.
Meta class
annotate(args - *kwargs)
Entry.objects.filter(id__gt=4)
choices tuple
28. Lookup type for date/datetime fields that finds an exact year match. Takes a four-digit year.
exact
year
iendswith
select_related()
29. This query finds all entries between a start date of start_date and an end date of end_date.
endswith
istartswith
extra(select=None - where=None - params=None - tables=None - order_by=None - select_params=None)
Entry.objects.filter(pub_date__range=(start_date - end_date))
30. Conjuntion operator for Q objects.
&
Many-to-many relationship to self
|
istartswith
31. 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.
ManyToManyField
reverse()
count()
day
32. (1) These cannot be Python reserved words - because that would result in a Python syntax error. (2) These cannot contain more than one underscore in a row - due to the way Django's query lookup syntax works.
search
unique
Q()
Restrictions on field names
33. 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
Q()
~
update(**kwargs)
Entry.objects.filter(pub_date__year=2005).delete()
34. 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.
ordered
Q()
search
Many-to-many relationship to self
35. If this option is True - the field is allowed to be blank. Default is False.
blank
select_related()
endswith
class attributes
36. Lookup type that yields a case-insensitive match.
only(*fields)
select_related()
iexact
Manager methods
37. This method immediately deletes the object and has no return value.
delete()
class attributes
ForeignKey
DoesNotExist
38. This gives your model metadata.
F()
Meta class
istartswith
Many-to-many relationship through an intermediate model
39. Keyword shortcut for looking up an object by primary key.
pk
Entry.objects.all().update(n_pingbacks=F('n_pingbacks') + 1)
pickling
defer(*fields)
40. Lookup type that returns results that fall into an inclusive date range.
ManyToManyField
Abstract base class
range
Custom model methods
41. Lookup type that returns results with a case-insensitive start sequence.
all()
==
related_name
istartswith
42. Returns True if the QuerySet contains any results - and False if not. This tries to perform the query in the simplest and fastest way possible - but it does execute nearly the same query. This means that calling this method on a queryset is faster th
id field
iterator()
Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')
exists()
43. Takes a list of primary-key values and returns a dictionary mapping each primary-key value to an instance of the object with the given ID.
defer(*fields)
in_bulk(id_list)
filter(**kwargs)
many-to-one relationship
44. This represents a collection of objects from your database. It can have zero - one or many filters.
many-to-one relationship
startswith
QuerySet
choices tuple
45. A Q object that encapsulates queries for entries with a question value that starts with 'What' in a case-insensitive fashion.
Warning
: Invalid argument supplied for foreach() in
/var/www/html/basicversity.com/show_quiz.php
on line
183
46. Returns the most recent object in the table - by date - using the field_name provided as the date field.
get(**kwargs)
unique
latest(field_name=None)
edit your settings file and change the INSTALLED_APPS setting to add the name of the module that contains your models.py.
47. Lookup type that returns results in a given list.
choices
Meta class
in
Many-to-many relationship through an intermediate model
48. 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
update(**kwargs)
range
db
none()
49. This query deletes all Entry objects with a pub_date year of 2005.
Entry.objects.filter(pub_date__year=2005).delete()
clear() method
Entry.objects.filter(id__gt=4)
many-to-many relationship
50. This style of inheritanc is useful when you're subclassing an existing model (perhaps something from another application entirely) and want each model to have its own database table. Here - each model in the hierarchy is a model all by itself.
order_by(*fields)
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
Multi-table inheritance
count()