mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #15363 -- Renamed and normalized to get_queryset
the methods that return a QuerySet.
This commit is contained in:
parent
477d737e1e
commit
6983a1a540
46 changed files with 588 additions and 284 deletions
|
@ -703,7 +703,7 @@ subclass::
|
|||
Only show the lookups if there actually is
|
||||
anyone born in the corresponding decades.
|
||||
"""
|
||||
qs = model_admin.queryset(request)
|
||||
qs = model_admin.get_queryset(request)
|
||||
if qs.filter(birthday__gte=date(1980, 1, 1),
|
||||
birthday__lte=date(1989, 12, 31)).exists():
|
||||
yield ('80s', _('in the eighties'))
|
||||
|
@ -1326,20 +1326,23 @@ templates used by the :class:`ModelAdmin` views:
|
|||
be interpreted as meaning that the current user is not permitted to delete
|
||||
any object of this type).
|
||||
|
||||
.. method:: ModelAdmin.queryset(self, request)
|
||||
.. method:: ModelAdmin.get_queryset(self, request)
|
||||
|
||||
The ``queryset`` method on a ``ModelAdmin`` returns a
|
||||
The ``get_queryset`` method on a ``ModelAdmin`` returns a
|
||||
:class:`~django.db.models.query.QuerySet` of all model instances that
|
||||
can be edited by the admin site. One use case for overriding this method
|
||||
is to show objects owned by the logged-in user::
|
||||
|
||||
class MyModelAdmin(admin.ModelAdmin):
|
||||
def queryset(self, request):
|
||||
qs = super(MyModelAdmin, self).queryset(request)
|
||||
def get_queryset(self, request):
|
||||
qs = super(MyModelAdmin, self).get_queryset(request)
|
||||
if request.user.is_superuser:
|
||||
return qs
|
||||
return qs.filter(author=request.user)
|
||||
|
||||
.. versionchanged:: 1.6
|
||||
The ``get_queryset`` method was previously named ``queryset``.
|
||||
|
||||
.. method:: ModelAdmin.message_user(request, message, level=messages.INFO, extra_tags='', fail_silently=False)
|
||||
|
||||
Sends a message to the user using the :mod:`django.contrib.messages`
|
||||
|
@ -1549,7 +1552,7 @@ adds some of its own (the shared features are actually defined in the
|
|||
- :attr:`~ModelAdmin.filter_vertical`
|
||||
- :attr:`~ModelAdmin.ordering`
|
||||
- :attr:`~ModelAdmin.prepopulated_fields`
|
||||
- :meth:`~ModelAdmin.queryset`
|
||||
- :meth:`~ModelAdmin.get_queryset`
|
||||
- :attr:`~ModelAdmin.radio_fields`
|
||||
- :attr:`~ModelAdmin.readonly_fields`
|
||||
- :attr:`~InlineModelAdmin.raw_id_fields`
|
||||
|
|
|
@ -1586,32 +1586,32 @@ The most efficient method of finding whether a model with a unique field
|
|||
(e.g. ``primary_key``) is a member of a :class:`.QuerySet` is::
|
||||
|
||||
entry = Entry.objects.get(pk=123)
|
||||
if some_query_set.filter(pk=entry.pk).exists():
|
||||
if some_queryset.filter(pk=entry.pk).exists():
|
||||
print("Entry contained in queryset")
|
||||
|
||||
Which will be faster than the following which requires evaluating and iterating
|
||||
through the entire queryset::
|
||||
|
||||
if entry in some_query_set:
|
||||
if entry in some_queryset:
|
||||
print("Entry contained in QuerySet")
|
||||
|
||||
And to find whether a queryset contains any items::
|
||||
|
||||
if some_query_set.exists():
|
||||
print("There is at least one object in some_query_set")
|
||||
if some_queryset.exists():
|
||||
print("There is at least one object in some_queryset")
|
||||
|
||||
Which will be faster than::
|
||||
|
||||
if some_query_set:
|
||||
print("There is at least one object in some_query_set")
|
||||
if some_queryset:
|
||||
print("There is at least one object in some_queryset")
|
||||
|
||||
... but not by a large degree (hence needing a large queryset for efficiency
|
||||
gains).
|
||||
|
||||
Additionally, if a ``some_query_set`` has not yet been evaluated, but you know
|
||||
that it will be at some point, then using ``some_query_set.exists()`` will do
|
||||
Additionally, if a ``some_queryset`` has not yet been evaluated, but you know
|
||||
that it will be at some point, then using ``some_queryset.exists()`` will do
|
||||
more overall work (one query for the existence check plus an extra one to later
|
||||
retrieve the results) than simply using ``bool(some_query_set)``, which
|
||||
retrieve the results) than simply using ``bool(some_queryset)``, which
|
||||
retrieves the results and then checks if any were returned.
|
||||
|
||||
update
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue