1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #25508 -- Modified QuerySet.__repr__() to disambiguate it from a list.

This commit is contained in:
Tim Graham
2015-10-05 19:07:34 -04:00
parent 3543fec3b7
commit e0837f2cb1
20 changed files with 185 additions and 184 deletions

View File

@@ -382,7 +382,7 @@ be used to retrieve their associated ``TaggedItems``::
>>> t2 = TaggedItem(content_object=b, tag='python')
>>> t2.save()
>>> b.tags.all()
[<TaggedItem: django>, <TaggedItem: python>]
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
Defining :class:`~django.contrib.contenttypes.fields.GenericRelation` with
``related_query_name`` set allows querying from the related object::
@@ -394,7 +394,7 @@ from ``TaggedItem``::
>>> # Get all tags belonging to books containing `django` in the url
>>> TaggedItem.objects.filter(bookmarks__url__contains='django')
[<TaggedItem: django>, <TaggedItem: python>]
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
Just as :class:`~django.contrib.contenttypes.fields.GenericForeignKey`
accepts the names of the content-type and object-ID fields as
@@ -418,7 +418,7 @@ same types of lookups manually::
>>> bookmark_type = ContentType.objects.get_for_model(b)
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id,
... object_id=b.id)
[<TaggedItem: django>, <TaggedItem: python>]
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
Note that if the model in a
:class:`~django.contrib.contenttypes.fields.GenericRelation` uses a

View File

@@ -588,9 +588,8 @@ Consortium (OGC). [#]_ Import the ``WorldBorder`` model, and perform
a ``contains`` lookup using the ``pnt_wkt`` as the parameter::
>>> from world.models import WorldBorder
>>> qs = WorldBorder.objects.filter(mpoly__contains=pnt_wkt)
>>> qs
[<WorldBorder: United States>]
>>> WorldBorder.objects.filter(mpoly__contains=pnt_wkt)
<QuerySet [<WorldBorder: United States>]>
Here, you retrieved a ``QuerySet`` with only one model: the border of the
United States (exactly what you would expect).
@@ -602,8 +601,7 @@ of a queryset::
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(12.4604, 43.9420)
>>> sm = WorldBorder.objects.get(mpoly__intersects=pnt)
>>> sm
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
<WorldBorder: San Marino>
The ``contains`` and ``intersects`` lookups are just a subset of the
@@ -638,7 +636,7 @@ of abstraction::
"world_worldborder"."mpoly" FROM "world_worldborder"
WHERE ST_Intersects("world_worldborder"."mpoly", ST_Transform(%s, 4326))
>>> qs # printing evaluates the queryset
[<WorldBorder: United States>]
<QuerySet [<WorldBorder: United States>]>
__ http://spatialreference.org/ref/epsg/32140/

View File

@@ -120,13 +120,13 @@ data. It uses the SQL operator ``@>``. For example::
>>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])
>>> Post.objects.filter(tags__contains=['thoughts'])
[<Post: First post>, <Post: Second post>]
<QuerySet [<Post: First post>, <Post: Second post>]>
>>> Post.objects.filter(tags__contains=['django'])
[<Post: First post>, <Post: Third post>]
<QuerySet [<Post: First post>, <Post: Third post>]>
>>> Post.objects.filter(tags__contains=['django', 'thoughts'])
[<Post: First post>]
<QuerySet [<Post: First post>]>
.. fieldlookup:: arrayfield.contained_by
@@ -142,10 +142,10 @@ passed. It uses the SQL operator ``<@``. For example::
>>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])
>>> Post.objects.filter(tags__contained_by=['thoughts', 'django'])
[<Post: First post>, <Post: Second post>]
<QuerySet [<Post: First post>, <Post: Second post>]>
>>> Post.objects.filter(tags__contained_by=['thoughts', 'django', 'tutorial'])
[<Post: First post>, <Post: Second post>, <Post: Third post>]
<QuerySet [<Post: First post>, <Post: Second post>, <Post: Third post>]>
.. fieldlookup:: arrayfield.overlap
@@ -160,10 +160,10 @@ the SQL operator ``&&``. For example::
>>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])
>>> Post.objects.filter(tags__overlap=['thoughts'])
[<Post: First post>, <Post: Second post>]
<QuerySet [<Post: First post>, <Post: Second post>]>
>>> Post.objects.filter(tags__overlap=['thoughts', 'tutorial'])
[<Post: First post>, <Post: Second post>, <Post: Third post>]
<QuerySet [<Post: First post>, <Post: Second post>, <Post: Third post>]>
.. fieldlookup:: arrayfield.len
@@ -177,7 +177,7 @@ available for :class:`~django.db.models.IntegerField`. For example::
>>> Post.objects.create(name='Second post', tags=['thoughts'])
>>> Post.objects.filter(tags__len=1)
[<Post: Second post>]
<QuerySet [<Post: Second post>]>
.. fieldlookup:: arrayfield.index
@@ -194,13 +194,13 @@ example::
>>> Post.objects.create(name='Second post', tags=['thoughts'])
>>> Post.objects.filter(tags__0='thoughts')
[<Post: First post>, <Post: Second post>]
<QuerySet [<Post: First post>, <Post: Second post>]>
>>> Post.objects.filter(tags__1__iexact='Django')
[<Post: First post>]
<QuerySet [<Post: First post>]>
>>> Post.objects.filter(tags__276='javascript')
[]
<QuerySet []>
.. note::
@@ -222,10 +222,10 @@ lookups available after the transform do not change. For example::
>>> Post.objects.create(name='Third post', tags=['django', 'python', 'thoughts'])
>>> Post.objects.filter(tags__0_1=['thoughts'])
[<Post: First post>]
<QuerySet [<Post: First post>]>
>>> Post.objects.filter(tags__0_2__contains='thoughts')
[<Post: First post>, <Post: Second post>]
<QuerySet [<Post: First post>, <Post: Second post>]>
.. note::
@@ -320,12 +320,12 @@ To query based on a given key, you simply use that key as the lookup name::
>>> Dog.objects.create(name='Meg', data={'breed': 'collie'})
>>> Dog.objects.filter(data__breed='collie')
[<Dog: Meg>]
<QuerySet [<Dog: Meg>]>
You can chain other lookups after key lookups::
>>> Dog.objects.filter(data__breed__contains='l')
[<Dog: Rufus>, <Dog: Meg>]
<QuerySet [<Dog: Rufus>, <Dog: Meg>]>
If the key you wish to query by clashes with the name of another lookup, you
need to use the :lookup:`hstorefield.contains` lookup instead.
@@ -352,10 +352,10 @@ field. It uses the SQL operator ``@>``. For example::
>>> Dog.objects.create(name='Fred', data={})
>>> Dog.objects.filter(data__contains={'owner': 'Bob'})
[<Dog: Rufus>, <Dog: Meg>]
<QuerySet [<Dog: Rufus>, <Dog: Meg>]>
>>> Dog.objects.filter(data__contains={'breed': 'collie'})
[<Dog: Meg>]
<QuerySet [<Dog: Meg>]>
.. fieldlookup:: hstorefield.contained_by
@@ -372,10 +372,10 @@ example::
>>> Dog.objects.create(name='Fred', data={})
>>> Dog.objects.filter(data__contained_by={'breed': 'collie', 'owner': 'Bob'})
[<Dog: Meg>, <Dog: Fred>]
<QuerySet [<Dog: Meg>, <Dog: Fred>]>
>>> Dog.objects.filter(data__contained_by={'breed': 'collie'})
[<Dog: Fred>]
<QuerySet [<Dog: Fred>]>
.. fieldlookup:: hstorefield.has_key
@@ -389,7 +389,7 @@ Returns objects where the given key is in the data. Uses the SQL operator
>>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
>>> Dog.objects.filter(data__has_key='owner')
[<Dog: Meg>]
<QuerySet [<Dog: Meg>]>
.. fieldlookup:: hstorefield.has_any_keys
@@ -406,7 +406,7 @@ operator ``?|``. For example::
>>> Dog.objects.create(name='Fred', data={})
>>> Dog.objects.filter(data__has_any_keys=['owner', 'breed'])
[<Dog: Rufus>, <Dog: Meg>]
<QuerySet [<Dog: Rufus>, <Dog: Meg>]>
.. fieldlookup:: hstorefield.has_keys
@@ -420,7 +420,7 @@ Returns objects where all of the given keys are in the data. Uses the SQL operat
>>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
>>> Dog.objects.filter(data__has_keys=['breed', 'owner'])
[<Dog: Meg>]
<QuerySet [<Dog: Meg>]>
.. fieldlookup:: hstorefield.keys
@@ -437,7 +437,7 @@ in conjunction with lookups on
>>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
>>> Dog.objects.filter(data__keys__overlap=['breed', 'toy'])
[<Dog: Rufus>, <Dog: Meg>]
<QuerySet [<Dog: Rufus>, <Dog: Meg>]>
.. fieldlookup:: hstorefield.values
@@ -454,7 +454,7 @@ using in conjunction with lookups on
>>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
>>> Dog.objects.filter(data__values__contains=['collie'])
[<Dog: Meg>]
<QuerySet [<Dog: Meg>]>
JSONField
---------
@@ -521,18 +521,18 @@ name::
>>> Dog.objects.create(name='Meg', data={'breed': 'collie'})
>>> Dog.objects.filter(data__breed='collie')
[<Dog: Meg>]
<QuerySet [<Dog: Meg>]>
Multiple keys can be chained together to form a path lookup::
>>> Dog.objects.filter(data__owner__name='Bob')
[<Dog: Rufus>]
<QuerySet [<QuerySet <Dog: Rufus>]>
If the key is an integer, it will be interpreted as an index lookup in an
array::
>>> Dog.objects.filter(data__owner__other_pets__0__name='Fishy')
[<Dog: Rufus>]
<QuerySet [<Dog: Rufus>]>
If the key you wish to query by clashes with the name of another lookup, use
the :lookup:`jsonfield.contains` lookup instead.
@@ -673,7 +673,7 @@ contains
''''''''
>>> Event.objects.filter(ages__contains=NumericRange(4, 5))
[<Event: Soft play>]
<QuerySet [<Event: Soft play>]>
.. fieldlookup:: rangefield.contained_by
@@ -681,7 +681,7 @@ contained_by
''''''''''''
>>> Event.objects.filter(ages__contained_by=NumericRange(0, 15))
[<Event: Soft play>]
<QuerySet [<Event: Soft play>]>
.. versionadded 1.9
@@ -697,7 +697,7 @@ contained_by
... timezone.now() - datetime.timedelta(hours=1),
... timezone.now() + datetime.timedelta(hours=1),
... )
[<Event: Soft play>]
<QuerySet [<Event: Soft play>]>
.. fieldlookup:: rangefield.overlap
@@ -705,7 +705,7 @@ overlap
'''''''
>>> Event.objects.filter(ages__overlap=NumericRange(8, 12))
[<Event: Soft play>]
<QuerySet [<Event: Soft play>]>
Comparison functions
~~~~~~~~~~~~~~~~~~~~
@@ -726,7 +726,7 @@ all the points in the returned range are less than all those in the passed
range.
>>> Event.objects.filter(ages__fully_lt=NumericRange(11, 15))
[<Event: Soft play>]
<QuerySet [<Event: Soft play>]>
.. fieldlookup:: rangefield.fully_gt
@@ -738,7 +738,7 @@ the all the points in the returned range are greater than all those in the
passed range.
>>> Event.objects.filter(ages__fully_gt=NumericRange(11, 15))
[<Event: Pub trip>]
<QuerySet [<Event: Pub trip>]>
.. fieldlookup:: rangefield.not_lt
@@ -750,7 +750,7 @@ is the lower bound of the returned range is at least the lower bound of the
passed range.
>>> Event.objects.filter(ages__not_lt=NumericRange(0, 15))
[<Event: Soft play>, <Event: Pub trip>]
<QuerySet [<Event: Soft play>, <Event: Pub trip>]>
.. fieldlookup:: rangefield.not_gt
@@ -762,7 +762,7 @@ is the upper bound of the returned range is at most the upper bound of the
passed range.
>>> Event.objects.filter(ages__not_gt=NumericRange(3, 10))
[<Event: Soft play>]
<QuerySet [<Event: Soft play>]>
.. fieldlookup:: rangefield.adjacent_to
@@ -772,7 +772,7 @@ adjacent_to
The returned ranges share a bound with the passed range.
>>> Event.objects.filter(ages__adjacent_to=NumericRange(10, 21))
[<Event: Soft play>, <Event: Pub trip>]
<QuerySet [<Event: Soft play>, <Event: Pub trip>]>
Querying using the bounds
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -789,7 +789,7 @@ Returned objects have the given lower bound. Can be chained to valid lookups
for the base field.
>>> Event.objects.filter(ages__startswith=21)
[<Event: Pub trip>]
<QuerySet [<Event: Pub trip>]>
.. fieldlookup:: rangefield.endswith
@@ -800,7 +800,7 @@ Returned objects have the given upper bound. Can be chained to valid lookups
for the base field.
>>> Event.objects.filter(ages__endswith=10)
[<Event: Soft play>]
<QuerySet [<Event: Soft play>]>
.. fieldlookup:: rangefield.isempty
@@ -811,7 +811,7 @@ Returned objects are empty ranges. Can be chained to valid lookups for a
:class:`~django.db.models.BooleanField`.
>>> Event.objects.filter(ages__isempty=True)
[]
<QuerySet []>
Defining your own range types
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -28,4 +28,4 @@ Usage example::
>>> from django.contrib.postgres.functions import TransactionNow
>>> Article.objects.filter(published__lte=TransactionNow())
[<Article: How to Django>]
<QuerySet [<Article: How to Django>]>

View File

@@ -226,7 +226,7 @@ Usage example::
>>> from django.db.models.functions import Now
>>> Article.objects.filter(published__lte=Now())
[<Article: How to Django>]
<QuerySet [<Article: How to Django>]>
.. admonition:: PostgreSQL considerations

View File

@@ -513,11 +513,11 @@ objects::
# This list contains a Blog object.
>>> Blog.objects.filter(name__startswith='Beatles')
[<Blog: Beatles Blog>]
<QuerySet [<Blog: Beatles Blog>]>
# This list contains a dictionary.
>>> Blog.objects.filter(name__startswith='Beatles').values()
[{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]
<QuerySet [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]>
The ``values()`` method takes optional positional arguments, ``*fields``, which
specify field names to which the ``SELECT`` should be limited. If you specify
@@ -528,9 +528,9 @@ contain a key and value for every field in the database table.
Example::
>>> Blog.objects.values()
[{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],
<QuerySet [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]>
>>> Blog.objects.values('id', 'name')
[{'id': 1, 'name': 'Beatles Blog'}]
<QuerySet [{'id': 1, 'name': 'Beatles Blog'}]>
A few subtleties that are worth mentioning:
@@ -546,13 +546,13 @@ A few subtleties that are worth mentioning:
For example::
>>> Entry.objects.values()
[{'blog_id': 1, 'headline': 'First Entry', ...}, ...]
<QuerySet [{'blog_id': 1, 'headline': 'First Entry', ...}, ...]>
>>> Entry.objects.values('blog')
[{'blog': 1}, ...]
<QuerySet [{'blog': 1}, ...]>
>>> Entry.objects.values('blog_id')
[{'blog_id': 1}, ...]
<QuerySet [{'blog_id': 1}, ...]>
* When using ``values()`` together with :meth:`distinct()`, be aware that
ordering can affect the results. See the note in :meth:`distinct` for
@@ -585,9 +585,9 @@ individualism.
You can also refer to fields on related models with reverse relations through
``OneToOneField``, ``ForeignKey`` and ``ManyToManyField`` attributes::
Blog.objects.values('name', 'entry__headline')
[{'name': 'My blog', 'entry__headline': 'An entry'},
{'name': 'My blog', 'entry__headline': 'Another entry'}, ...]
>>> Blog.objects.values('name', 'entry__headline')
<QuerySet [{'name': 'My blog', 'entry__headline': 'An entry'},
{'name': 'My blog', 'entry__headline': 'Another entry'}, ...]>
.. warning::
@@ -717,7 +717,7 @@ is an instance of ``EmptyQuerySet``.
Examples::
>>> Entry.objects.none()
[]
<QuerySet []>
>>> from django.db.models.query import EmptyQuerySet
>>> isinstance(Entry.objects.none(), EmptyQuerySet)
True
@@ -3015,11 +3015,11 @@ as the string based lookups passed to
:meth:`~django.db.models.query.QuerySet.prefetch_related()`. For example:
>>> Question.objects.prefetch_related(Prefetch('choice_set')).get().choice_set.all()
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
# This will only execute two queries regardless of the number of Question
# and Choice objects.
>>> Question.objects.prefetch_related(Prefetch('choice_set')).all()
[<Question: Question object>]
<QuerySet [<Question: Question object>]>
The ``queryset`` argument supplies a base ``QuerySet`` for the given lookup.
This is useful to further filter down the prefetch operation, or to call
@@ -3028,19 +3028,19 @@ relation, hence reducing the number of queries even further:
>>> voted_choices = Choice.objects.filter(votes__gt=0)
>>> voted_choices
[<Choice: The sky>]
<QuerySet [<Choice: The sky>]>
>>> prefetch = Prefetch('choice_set', queryset=voted_choices)
>>> Question.objects.prefetch_related(prefetch).get().choice_set.all()
[<Choice: The sky>]
<QuerySet [<Choice: The sky>]>
The ``to_attr`` argument sets the result of the prefetch operation to a custom
attribute:
>>> prefetch = Prefetch('choice_set', queryset=voted_choices, to_attr='voted_choices')
>>> Question.objects.prefetch_related(prefetch).get().voted_choices
[<Choice: The sky>]
<QuerySet [<Choice: The sky>]>
>>> Question.objects.prefetch_related(prefetch).get().choice_set.all()
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
.. note::