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. restrictions on ________: (1) Your intermediate model must contain one - and only one - foreign key to the target model. (2) Your intermediate model must contain one - and only one - foreign key to the source model. (3) When defining a many-to-many r
intermediate models
select_related()
range
pk
2. Lookup type that returns results greater than or equal to a given value.
many-to-many relationship
Many-to-many relationship to self
regex
gte
3. 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.
Multi-table inheritance
iregex
contains
|
4. Lookup type for date/datetime fields that finds a 'day of the week' match.
pickling
iteration
week_day
help_text
5. Lookup type for date/datetime fields that finds an exact month match. Takes an integer 1 (January) through 12
choices
many-to-many relationship
clear() method
month
6. If you pickle a QuerySet - this will force all the results to be loaded into memory prior to pickling. When you unpickle a QuerySet - it contains the results at the moment it was pickled - rather than the results that are currently in the database.
pickling
unique
OneToOneField
Q()
7. 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
8. Operator for comparing two model instances for equality. Behind the scenes - it compares the primary key values of two models.
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
Entry.objects.filter(pub_date__year=2005).delete()
==
many-to-one relationship
9. If you are using this attribute on a ForeignKey or ManyToManyField - you must always specify a unique reverse name for the field.
related_name
Many-to-many relationship through an intermediate model
|
one-to-one relationship
10. Exception raised by get(**kwargs) if no items match the query.
values_list(*fields)
Abstract base class
Entry.objects.all().update(n_pingbacks=F('n_pingbacks') + 1)
DoesNotExist
11. The first element in this iterable is the value that will be stored in the database - the second element will be displayed by the admin interface - or in a ModelChoiceField.
Field lookups
intermediate models
choices tuple
search
12. Lookup type that returns results less than a given value.
iregex
get(**kwargs)
lt
get_or_create(**kwargs)
13. This field is added automatically - but this behavior can be overridden
ordered
pk
id field
Entry.objects.all().update(n_pingbacks=F('n_pingbacks') + 1)
14. 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
15. 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
help_text
isnull
contains
Abstract base class
16. 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
update(**kwargs)
save()
gte
through argument
17. Lookup type that returns results greater than a given value.
model
reverse()
aggregate(args - *kwargs)
gt
18. When to run syncdb
count()
Manager methods
Q(question__istartswith='What')
when you add new apps to INSTALLED_APPS
19. Lookup type that returns results in a given list.
in
save()
Custom model methods
Many-to-many relationship through an intermediate model
20. 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)
Abstract base class
filter chaining
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
21. 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()
intermediate models
class attributes
select_related()
22. These add custom "row-level" functionality to your objects. These act on a particular model instance.
Model metadata
Manager
Custom model methods
extra(select=None - where=None - params=None - tables=None - order_by=None - select_params=None)
23. Lookup type that finds a case-insensitive regular expression match.
DoesNotExist
iregex
annotate(args - *kwargs)
help_text
24. 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
25. Returns the most recent object in the table - by date - using the field_name provided as the date field.
latest(field_name=None)
intermediate models
many-to-one relationship
Q(question__istartswith='What')
26. Lookup type that returns results with a case-sensitive end sequence.
endswith
Entry.objects.filter(id__gt=4)
Entry.objects.all().update(n_pingbacks=F('n_pingbacks') + 1)
Manager methods
27. A Manager method that returns a new QuerySet containing objects that match the given lookup parameters.
filter(**kwargs)
annotate(args - *kwargs)
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
Q()
28. This tells Django how to calculate the URL for an object. Django uses this in its admin interface - and any time it needs to figure out a URL for an object.
aggregate(args - *kwargs)
get_absolute_url()
values(*fields)
update()
29. 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
all()
relationship spanning
Many-to-many relationship through an intermediate model
ManyToManyField
30. 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
&
exists()
all()
filter(**kwargs)
31. Manager method used to retrieve every object in a model.
dates(field - kind - order='ASC')
class attributes
Field lookups
all()
32. Each one of these is a Python class that subclasses django.db.models.Model. Each attribute of one of these represents a database field.
primary_key
class attributes
exclude(**kwargs)
model
33. 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
&
iexact
34. In some complex data-modeling situations - your models might contain a lot of fields - some of which could contain a lot of data (for example - text fields) - or require expensive processing to convert them to Python objects. If you are using the res
&
defer(*fields)
get_or_create(**kwargs)
primary_key
35. Lookup type that returns results with a case-insensitive start sequence.
Restrictions on field names
ManyToManyField
Q()
istartswith
36. This query finds all entries with an id in the list [1 - 3 - 4]
defer(*fields)
exists()
help_text
Entry.objects.filter(id__in=[1 - 3 - 4])
37. Disjunction operator for Q objects.
|
gte
exclude(**kwargs)
all()
38. Returns an integer representing the number of objects in the database matching the QuerySet. This never raises exceptions.
filter
count()
intermediate models
clear() method
39. This query deletes all Entry objects with a pub_date year of 2005.
reverse name
evaluation
Entry.objects.filter(pub_date__year=2005).delete()
Q()
40. A Python "magic method" that returns a unicode "representation" of any object.
annotate(args - *kwargs)
startswith
__unicode__()
Q(question__startswith='Who') | ~Q(pub_date__year=2005)
41. Returns a dictionary of aggregate values (averages - sums - etc) calculated over the QuerySet. Each argument to this method specifies a value that will be included in the dictionary that is returned.
many-to-many relationship
MultipleObjectsReturned
aggregate(args - *kwargs)
exclude(**kwargs)
42. This model method is used for updating a ManyToManyField.
related_name
Entry.objects.filter(id__gt=4)
through argument
add()
43. These are specified as keyword arguments to the QuerySet methods filter() - exclude() and get(). These take the form field__lookuptype=value .
get_absolute_url()
Abstract base class
reverse()
Field lookups
44. 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.
Entry.objects.filter(pub_date__year=2005).delete()
class type
filter chaining
help_text
45. Defined by a ManyToManyField. You use it just like any other Field type: by including it as a class attribute of your model.
month
null
filter chaining
many-to-many relationship
46. Lookup type that returns results less than or equal to a given value.
ManyToManyField
evaluation
lte
get_absolute_url()
47. A manager method that returns a new QuerySet containing objects that do not match the given lookup parameters.
reverse name
week_day
exclude(**kwargs)
Abstract base class
48. 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.
Entry.objects.filter(id__in=[1 - 3 - 4])
order_by(*fields)
id field
filter(**kwargs)
49. Lookup type for date/datetime fields that finds an exact year match. Takes a four-digit year.
null
add()
year
help_text
50. 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
contains
Proxy model
Many-to-many relationship to self
none()