Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #15363 -- Renamed and normalized to `get_queryset` the methods …

…that return a QuerySet.
  • Loading branch information...
commit 6983a1a540a6e6c3bd941fa15ddd8cb49f9ec74e 1 parent 477d737
@loic loic authored charettes committed
Showing with 588 additions and 284 deletions.
  1. +1 −1  AUTHORS
  2. +19 −12 django/contrib/admin/options.py
  3. +5 −5 django/contrib/admin/templatetags/admin_list.py
  4. +31 −8 django/contrib/admin/views/main.py
  5. +1 −1  django/contrib/auth/admin.py
  6. +2 −2 django/contrib/comments/managers.py
  7. +13 −4 django/contrib/comments/templatetags/comments.py
  8. +14 −6 django/contrib/contenttypes/generic.py
  9. +31 −31 django/contrib/gis/db/models/manager.py
  10. +2 −2 django/contrib/sites/managers.py
  11. +1 −1  django/core/serializers/__init__.py
  12. +27 −19 django/db/models/fields/related.py
  13. +46 −38 django/db/models/manager.py
  14. +7 −7 django/db/models/query.py
  15. +3 −3 django/forms/models.py
  16. +62 −0 django/utils/deprecation.py
  17. +1 −1  docs/faq/admin.txt
  18. +9 −0 docs/internals/deprecation.txt
  19. +9 −6 docs/ref/contrib/admin/index.txt
  20. +9 −9 docs/ref/models/querysets.txt
  21. +6 −0 docs/releases/1.6.txt
  22. +14 −11 docs/topics/db/managers.txt
  23. +8 −8 docs/topics/db/multi-db.txt
  24. +4 −4 tests/admin_changelist/admin.py
  25. +2 −2 tests/admin_changelist/models.py
  26. +7 −7 tests/admin_changelist/tests.py
  27. +25 −25 tests/admin_filters/tests.py
  28. +10 −10 tests/admin_ordering/tests.py
  29. +18 −18 tests/admin_views/admin.py
  30. +2 −2 tests/admin_views/customadmin.py
  31. +6 −6 tests/admin_views/tests.py
  32. +2 −2 tests/admin_widgets/models.py
  33. +5 −5 tests/custom_managers/models.py
  34. +2 −2 tests/custom_managers_regress/models.py
  35. 0  tests/deprecation/__init__.py
  36. 0  tests/deprecation/models.py
  37. +158 −0 tests/deprecation/tests.py
  38. +2 −2 tests/fixtures/models.py
  39. +2 −2 tests/generic_relations/models.py
  40. +2 −2 tests/get_object_or_404/models.py
  41. +6 −6 tests/managers_regress/models.py
  42. +2 −2 tests/modeladmin/tests.py
  43. +2 −2 tests/prefetch_related/models.py
  44. +4 −4 tests/proxy_models/models.py
  45. +4 −4 tests/queries/models.py
  46. +2 −2 tests/reverse_single_related/models.py
View
2  AUTHORS
@@ -98,7 +98,7 @@ answer newbie questions, and generally made Django that much better:
Natalia Bidart <nataliabidart@gmail.com>
Mark Biggers <biggers@utsl.com>
Paul Bissex <http://e-scribe.com/>
- Loic Bistuer <loic.bistuer@sixmedia.com>
+ Loïc Bistuer <loic.bistuer@sixmedia.com>
Simon Blanchard
Craig Blaszczyk <masterjakul@gmail.com>
David Blewett <david@dawninglight.net>
View
31 django/contrib/admin/options.py
@@ -29,6 +29,7 @@
from django.utils.html import escape, escapejs
from django.utils.safestring import mark_safe
from django.utils import six
+from django.utils.deprecation import RenameMethodsBase
from django.utils.text import capfirst, get_text_list
from django.utils.translation import ugettext as _
from django.utils.translation import ungettext
@@ -64,7 +65,13 @@ class IncorrectLookupParameters(Exception):
csrf_protect_m = method_decorator(csrf_protect)
-class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
+class RenameBaseModelAdminMethods(forms.MediaDefiningClass, RenameMethodsBase):
+ renamed_methods = (
+ ('queryset', 'get_queryset', PendingDeprecationWarning),
+ )
+
+
+class BaseModelAdmin(six.with_metaclass(RenameBaseModelAdminMethods)):
"""Functionality common to both ModelAdmin and InlineAdmin."""
raw_id_fields = ()
@@ -239,12 +246,12 @@ def get_prepopulated_fields(self, request, obj=None):
"""
return self.prepopulated_fields
- def queryset(self, request):
+ def get_queryset(self, request):
"""
Returns a QuerySet of all model instances that can be edited by the
admin site. This is used by changelist_view.
"""
- qs = self.model._default_manager.get_query_set()
+ qs = self.model._default_manager.get_queryset()
# TODO: this should be handled by some parameter to the ChangeList.
ordering = self.get_ordering(request)
if ordering:
@@ -496,7 +503,7 @@ def get_object(self, request, object_id):
returned if no match is found (or the object_id failed validation
against the primary key field).
"""
- queryset = self.queryset(request)
+ queryset = self.get_queryset(request)
model = queryset.model
try:
object_id = model._meta.pk.to_python(object_id)
@@ -1008,7 +1015,7 @@ def add_view(self, request, form_url='', extra_context=None):
formset = FormSet(data=request.POST, files=request.FILES,
instance=new_object,
save_as_new="_saveasnew" in request.POST,
- prefix=prefix, queryset=inline.queryset(request))
+ prefix=prefix, queryset=inline.get_queryset(request))
formsets.append(formset)
if all_valid(formsets) and form_validated:
self.save_model(request, new_object, form, False)
@@ -1034,7 +1041,7 @@ def add_view(self, request, form_url='', extra_context=None):
if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=self.model(), prefix=prefix,
- queryset=inline.queryset(request))
+ queryset=inline.get_queryset(request))
formsets.append(formset)
adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
@@ -1104,7 +1111,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(request.POST, request.FILES,
instance=new_object, prefix=prefix,
- queryset=inline.queryset(request))
+ queryset=inline.get_queryset(request))
formsets.append(formset)
@@ -1124,7 +1131,7 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=obj, prefix=prefix,
- queryset=inline.queryset(request))
+ queryset=inline.get_queryset(request))
formsets.append(formset)
adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
@@ -1209,7 +1216,7 @@ def changelist_view(self, request, extra_context=None):
if (actions and request.method == 'POST' and
'index' in request.POST and '_save' not in request.POST):
if selected:
- response = self.response_action(request, queryset=cl.get_query_set(request))
+ response = self.response_action(request, queryset=cl.get_queryset(request))
if response:
return response
else:
@@ -1225,7 +1232,7 @@ def changelist_view(self, request, extra_context=None):
helpers.ACTION_CHECKBOX_NAME in request.POST and
'index' not in request.POST and '_save' not in request.POST):
if selected:
- response = self.response_action(request, queryset=cl.get_query_set(request))
+ response = self.response_action(request, queryset=cl.get_queryset(request))
if response:
return response
else:
@@ -1521,8 +1528,8 @@ def get_fieldsets(self, request, obj=None):
fields = list(form.base_fields) + list(self.get_readonly_fields(request, obj))
return [(None, {'fields': fields})]
- def queryset(self, request):
- queryset = super(InlineModelAdmin, self).queryset(request)
+ def get_queryset(self, request):
+ queryset = super(InlineModelAdmin, self).get_queryset(request)
if not self.has_change_permission(request):
queryset = queryset.none()
return queryset
View
10 django/contrib/admin/templatetags/admin_list.py
@@ -306,8 +306,8 @@ def date_hierarchy(cl):
if not (year_lookup or month_lookup or day_lookup):
# select appropriate start level
- date_range = cl.query_set.aggregate(first=models.Min(field_name),
- last=models.Max(field_name))
+ date_range = cl.queryset.aggregate(first=models.Min(field_name),
+ last=models.Max(field_name))
if date_range['first'] and date_range['last']:
if date_range['first'].year == date_range['last'].year:
year_lookup = date_range['first'].year
@@ -325,7 +325,7 @@ def date_hierarchy(cl):
'choices': [{'title': capfirst(formats.date_format(day, 'MONTH_DAY_FORMAT'))}]
}
elif year_lookup and month_lookup:
- days = cl.query_set.filter(**{year_field: year_lookup, month_field: month_lookup})
+ days = cl.queryset.filter(**{year_field: year_lookup, month_field: month_lookup})
days = getattr(days, dates_or_datetimes)(field_name, 'day')
return {
'show': True,
@@ -339,7 +339,7 @@ def date_hierarchy(cl):
} for day in days]
}
elif year_lookup:
- months = cl.query_set.filter(**{year_field: year_lookup})
+ months = cl.queryset.filter(**{year_field: year_lookup})
months = getattr(months, dates_or_datetimes)(field_name, 'month')
return {
'show': True,
@@ -353,7 +353,7 @@ def date_hierarchy(cl):
} for month in months]
}
else:
- years = getattr(cl.query_set, dates_or_datetimes)(field_name, 'year')
+ years = getattr(cl.queryset, dates_or_datetimes)(field_name, 'year')
return {
'show': True,
'choices': [{
View
39 django/contrib/admin/views/main.py
@@ -1,4 +1,5 @@
import operator
+import warnings
from functools import reduce
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
@@ -6,7 +7,9 @@
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.fields import FieldDoesNotExist
+from django.utils import six
from django.utils.datastructures import SortedDict
+from django.utils.deprecation import RenameMethodsBase
from django.utils.encoding import force_str, force_text
from django.utils.translation import ugettext, ugettext_lazy
from django.utils.http import urlencode
@@ -33,14 +36,20 @@
EMPTY_CHANGELIST_VALUE = ugettext_lazy('(None)')
-class ChangeList(object):
+class RenameChangeListMethods(RenameMethodsBase):
+ renamed_methods = (
+ ('get_query_set', 'get_queryset', PendingDeprecationWarning),
+ )
+
+
+class ChangeList(six.with_metaclass(RenameChangeListMethods)):
def __init__(self, request, model, list_display, list_display_links,
list_filter, date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable, model_admin):
self.model = model
self.opts = model._meta
self.lookup_opts = self.opts
- self.root_query_set = model_admin.queryset(request)
+ self.root_queryset = model_admin.get_queryset(request)
self.list_display = list_display
self.list_display_links = list_display_links
self.list_filter = list_filter
@@ -70,7 +79,7 @@ def __init__(self, request, model, list_display, list_display_links,
else:
self.list_editable = list_editable
self.query = request.GET.get(SEARCH_VAR, '')
- self.query_set = self.get_query_set(request)
+ self.queryset = self.get_queryset(request)
self.get_results(request)
if self.is_popup:
title = ugettext('Select %s')
@@ -79,6 +88,20 @@ def __init__(self, request, model, list_display, list_display_links,
self.title = title % force_text(self.opts.verbose_name)
self.pk_attname = self.lookup_opts.pk.attname
+ @property
+ def root_query_set(self):
+ warnings.warn("`ChangeList.root_query_set` is deprecated, "
+ "use `root_queryset` instead.",
+ PendingDeprecationWarning, 2)
+ return self.root_queryset
+
+ @property
+ def query_set(self):
+ warnings.warn("`ChangeList.query_set` is deprecated, "
+ "use `queryset` instead.",
+ PendingDeprecationWarning, 2)
+ return self.queryset
+
def get_filters_params(self, params=None):
"""
Returns all params except IGNORED_PARAMS
@@ -169,7 +192,7 @@ def get_query_string(self, new_params=None, remove=None):
return '?%s' % urlencode(sorted(p.items()))
def get_results(self, request):
- paginator = self.model_admin.get_paginator(request, self.query_set, self.list_per_page)
+ paginator = self.model_admin.get_paginator(request, self.queryset, self.list_per_page)
# Get the number of objects, with admin filters applied.
result_count = paginator.count
@@ -178,7 +201,7 @@ def get_results(self, request):
# full_result_count is equal to paginator.count if no filters
# were applied
if self.get_filters_params():
- full_result_count = self.root_query_set.count()
+ full_result_count = self.root_queryset.count()
else:
full_result_count = result_count
can_show_all = result_count <= self.list_max_show_all
@@ -186,7 +209,7 @@ def get_results(self, request):
# Get the list of objects to display on this page.
if (self.show_all and can_show_all) or not multi_page:
- result_list = self.query_set._clone()
+ result_list = self.queryset._clone()
else:
try:
result_list = paginator.page(self.page_num+1).object_list
@@ -304,13 +327,13 @@ def get_ordering_field_columns(self):
ordering_fields[idx] = 'desc' if pfx == '-' else 'asc'
return ordering_fields
- def get_query_set(self, request):
+ def get_queryset(self, request):
# First, we collect all the declared list filters.
(self.filter_specs, self.has_filters, remaining_lookup_params,
use_distinct) = self.get_filters(request)
# Then, we let every list filter modify the queryset to its liking.
- qs = self.root_query_set
+ qs = self.root_queryset
for filter_spec in self.filter_specs:
new_qs = filter_spec.queryset(request, qs)
if new_qs is not None:
View
2  django/contrib/auth/admin.py
@@ -118,7 +118,7 @@ def add_view(self, request, form_url='', extra_context=None):
def user_change_password(self, request, id, form_url=''):
if not self.has_change_permission(request):
raise PermissionDenied
- user = get_object_or_404(self.queryset(request), pk=id)
+ user = get_object_or_404(self.get_queryset(request), pk=id)
if request.method == 'POST':
form = self.change_password_form(user, request.POST)
if form.is_valid():
View
4 django/contrib/comments/managers.py
@@ -8,7 +8,7 @@ def in_moderation(self):
"""
QuerySet for all comments currently in the moderation queue.
"""
- return self.get_query_set().filter(is_public=False, is_removed=False)
+ return self.get_queryset().filter(is_public=False, is_removed=False)
def for_model(self, model):
"""
@@ -16,7 +16,7 @@ def for_model(self, model):
a class).
"""
ct = ContentType.objects.get_for_model(model)
- qs = self.get_query_set().filter(content_type=ct)
+ qs = self.get_queryset().filter(content_type=ct)
if isinstance(model, models.Model):
qs = qs.filter(object_pk=force_text(model._get_pk_val()))
return qs
View
17 django/contrib/comments/templatetags/comments.py
@@ -3,11 +3,20 @@
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib import comments
+from django.utils import six
+from django.utils.deprecation import RenameMethodsBase
from django.utils.encoding import smart_text
register = template.Library()
-class BaseCommentNode(template.Node):
+
+class RenameBaseCommentNodeMethods(RenameMethodsBase):
+ renamed_methods = (
+ ('get_query_set', 'get_queryset', PendingDeprecationWarning),
+ )
+
+
+class BaseCommentNode(six.with_metaclass(RenameBaseCommentNodeMethods, template.Node)):
"""
Base helper class (abstract) for handling the get_comment_* template tags.
Looks a bit strange, but the subclasses below should make this a bit more
@@ -64,11 +73,11 @@ def __init__(self, ctype=None, object_pk_expr=None, object_expr=None, as_varname
self.comment = comment
def render(self, context):
- qs = self.get_query_set(context)
+ qs = self.get_queryset(context)
context[self.as_varname] = self.get_context_value_from_queryset(context, qs)
return ''
- def get_query_set(self, context):
+ def get_queryset(self, context):
ctype, object_pk = self.get_target_ctype_pk(context)
if not object_pk:
return self.comment_model.objects.none()
@@ -205,7 +214,7 @@ def render(self, context):
"comments/%s/list.html" % ctype.app_label,
"comments/list.html"
]
- qs = self.get_query_set(context)
+ qs = self.get_queryset(context)
context.push()
liststr = render_to_string(template_search_list, {
"comment_list" : self.get_context_value_from_queryset(context, qs)
View
20 django/contrib/contenttypes/generic.py
@@ -16,10 +16,18 @@
from django.forms.models import BaseModelFormSet, modelformset_factory, save_instance
from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
from django.contrib.contenttypes.models import ContentType
+from django.utils import six
+from django.utils.deprecation import RenameMethodsBase
from django.utils.encoding import smart_text
-class GenericForeignKey(object):
+class RenameGenericForeignKeyMethods(RenameMethodsBase):
+ renamed_methods = (
+ ('get_prefetch_query_set', 'get_prefetch_queryset', PendingDeprecationWarning),
+ )
+
+
+class GenericForeignKey(six.with_metaclass(RenameGenericForeignKeyMethods)):
"""
Provides a generic relation to any object through content-type/object-id
fields.
@@ -60,7 +68,7 @@ def get_content_type(self, obj=None, id=None, using=None):
# This should never happen. I love comments like this, don't you?
raise Exception("Impossible arguments to GFK.get_content_type!")
- def get_prefetch_query_set(self, instances):
+ def get_prefetch_queryset(self, instances):
# For efficiency, group the instances by content type and then do one
# query per model
fk_dict = defaultdict(set)
@@ -316,21 +324,21 @@ def __init__(self, model=None, instance=None, symmetrical=None,
'%s__exact' % object_id_field_name: instance._get_pk_val(),
}
- def get_query_set(self):
+ def get_queryset(self):
try:
return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
except (AttributeError, KeyError):
db = self._db or router.db_for_read(self.model, instance=self.instance)
- return super(GenericRelatedObjectManager, self).get_query_set().using(db).filter(**self.core_filters)
+ return super(GenericRelatedObjectManager, self).get_queryset().using(db).filter(**self.core_filters)
- def get_prefetch_query_set(self, instances):
+ def get_prefetch_queryset(self, instances):
db = self._db or router.db_for_read(self.model, instance=instances[0])
query = {
'%s__pk' % self.content_type_field_name: self.content_type.id,
'%s__in' % self.object_id_field_name:
set(obj._get_pk_val() for obj in instances)
}
- qs = super(GenericRelatedObjectManager, self).get_query_set().using(db).filter(**query)
+ qs = super(GenericRelatedObjectManager, self).get_queryset().using(db).filter(**query)
# We (possibly) need to convert object IDs to the type of the
# instances' PK in order to match up instances:
object_id_converter = instances[0]._meta.pk.to_python
View
62 django/contrib/gis/db/models/manager.py
@@ -9,95 +9,95 @@ class GeoManager(Manager):
# properly.
use_for_related_fields = True
- def get_query_set(self):
+ def get_queryset(self):
return GeoQuerySet(self.model, using=self._db)
def area(self, *args, **kwargs):
- return self.get_query_set().area(*args, **kwargs)
+ return self.get_queryset().area(*args, **kwargs)
def centroid(self, *args, **kwargs):
- return self.get_query_set().centroid(*args, **kwargs)
+ return self.get_queryset().centroid(*args, **kwargs)
def collect(self, *args, **kwargs):
- return self.get_query_set().collect(*args, **kwargs)
+ return self.get_queryset().collect(*args, **kwargs)
def difference(self, *args, **kwargs):
- return self.get_query_set().difference(*args, **kwargs)
+ return self.get_queryset().difference(*args, **kwargs)
def distance(self, *args, **kwargs):
- return self.get_query_set().distance(*args, **kwargs)
+ return self.get_queryset().distance(*args, **kwargs)
def envelope(self, *args, **kwargs):
- return self.get_query_set().envelope(*args, **kwargs)
+ return self.get_queryset().envelope(*args, **kwargs)
def extent(self, *args, **kwargs):
- return self.get_query_set().extent(*args, **kwargs)
+ return self.get_queryset().extent(*args, **kwargs)
def extent3d(self, *args, **kwargs):
- return self.get_query_set().extent3d(*args, **kwargs)
+ return self.get_queryset().extent3d(*args, **kwargs)
def force_rhr(self, *args, **kwargs):
- return self.get_query_set().force_rhr(*args, **kwargs)
+ return self.get_queryset().force_rhr(*args, **kwargs)
def geohash(self, *args, **kwargs):
- return self.get_query_set().geohash(*args, **kwargs)
+ return self.get_queryset().geohash(*args, **kwargs)
def geojson(self, *args, **kwargs):
- return self.get_query_set().geojson(*args, **kwargs)
+ return self.get_queryset().geojson(*args, **kwargs)
def gml(self, *args, **kwargs):
- return self.get_query_set().gml(*args, **kwargs)
+ return self.get_queryset().gml(*args, **kwargs)
def intersection(self, *args, **kwargs):
- return self.get_query_set().intersection(*args, **kwargs)
+ return self.get_queryset().intersection(*args, **kwargs)
def kml(self, *args, **kwargs):
- return self.get_query_set().kml(*args, **kwargs)
+ return self.get_queryset().kml(*args, **kwargs)
def length(self, *args, **kwargs):
- return self.get_query_set().length(*args, **kwargs)
+ return self.get_queryset().length(*args, **kwargs)
def make_line(self, *args, **kwargs):
- return self.get_query_set().make_line(*args, **kwargs)
+ return self.get_queryset().make_line(*args, **kwargs)
def mem_size(self, *args, **kwargs):
- return self.get_query_set().mem_size(*args, **kwargs)
+ return self.get_queryset().mem_size(*args, **kwargs)
def num_geom(self, *args, **kwargs):
- return self.get_query_set().num_geom(*args, **kwargs)
+ return self.get_queryset().num_geom(*args, **kwargs)
def num_points(self, *args, **kwargs):
- return self.get_query_set().num_points(*args, **kwargs)
+ return self.get_queryset().num_points(*args, **kwargs)
def perimeter(self, *args, **kwargs):
- return self.get_query_set().perimeter(*args, **kwargs)
+ return self.get_queryset().perimeter(*args, **kwargs)
def point_on_surface(self, *args, **kwargs):
- return self.get_query_set().point_on_surface(*args, **kwargs)
+ return self.get_queryset().point_on_surface(*args, **kwargs)
def reverse_geom(self, *args, **kwargs):
- return self.get_query_set().reverse_geom(*args, **kwargs)
+ return self.get_queryset().reverse_geom(*args, **kwargs)
def scale(self, *args, **kwargs):
- return self.get_query_set().scale(*args, **kwargs)
+ return self.get_queryset().scale(*args, **kwargs)
def snap_to_grid(self, *args, **kwargs):
- return self.get_query_set().snap_to_grid(*args, **kwargs)
+ return self.get_queryset().snap_to_grid(*args, **kwargs)
def svg(self, *args, **kwargs):
- return self.get_query_set().svg(*args, **kwargs)
+ return self.get_queryset().svg(*args, **kwargs)
def sym_difference(self, *args, **kwargs):
- return self.get_query_set().sym_difference(*args, **kwargs)
+ return self.get_queryset().sym_difference(*args, **kwargs)
def transform(self, *args, **kwargs):
- return self.get_query_set().transform(*args, **kwargs)
+ return self.get_queryset().transform(*args, **kwargs)
def translate(self, *args, **kwargs):
- return self.get_query_set().translate(*args, **kwargs)
+ return self.get_queryset().translate(*args, **kwargs)
def union(self, *args, **kwargs):
- return self.get_query_set().union(*args, **kwargs)
+ return self.get_queryset().union(*args, **kwargs)
def unionagg(self, *args, **kwargs):
- return self.get_query_set().unionagg(*args, **kwargs)
+ return self.get_queryset().unionagg(*args, **kwargs)
View
4 django/contrib/sites/managers.py
@@ -35,7 +35,7 @@ def _validate_field_name(self):
(self.__class__.__name__, self.__field_name, self.model._meta.object_name))
self.__is_validated = True
- def get_query_set(self):
+ def get_queryset(self):
if not self.__is_validated:
self._validate_field_name()
- return super(CurrentSiteManager, self).get_query_set().filter(**{self.__field_name + '__id__exact': settings.SITE_ID})
+ return super(CurrentSiteManager, self).get_queryset().filter(**{self.__field_name + '__id__exact': settings.SITE_ID})
View
2  django/core/serializers/__init__.py
@@ -4,7 +4,7 @@
Usage::
from django.core import serializers
- json = serializers.serialize("json", some_query_set)
+ json = serializers.serialize("json", some_queryset)
objects = list(serializers.deserialize("json", json))
To add your own serializers, use the SERIALIZATION_MODULES setting::
View
46 django/db/models/fields/related.py
@@ -11,6 +11,7 @@
from django.db.models.deletion import CASCADE
from django.utils.encoding import smart_text
from django.utils import six
+from django.utils.deprecation import RenameMethodsBase
from django.utils.translation import ugettext_lazy as _, string_concat
from django.utils.functional import curry, cached_property
from django.core import exceptions
@@ -225,7 +226,14 @@ def related_query_name(self):
return self.rel.related_name or self.opts.model_name
-class SingleRelatedObjectDescriptor(object):
+class RenameRelatedObjectDescriptorMethods(RenameMethodsBase):
+ renamed_methods = (
+ ('get_query_set', 'get_queryset', PendingDeprecationWarning),
+ ('get_prefetch_query_set', 'get_prefetch_queryset', PendingDeprecationWarning),
+ )
+
+
+class SingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjectDescriptorMethods)):
# This class provides the functionality that makes the related-object
# managers available as attributes on a model class, for fields that have
# a single "remote" value, on the class pointed to by a related field.
@@ -238,16 +246,16 @@ def __init__(self, related):
def is_cached(self, instance):
return hasattr(instance, self.cache_name)
- def get_query_set(self, **db_hints):
+ def get_queryset(self, **db_hints):
db = router.db_for_read(self.related.model, **db_hints)
return self.related.model._base_manager.using(db)
- def get_prefetch_query_set(self, instances):
+ def get_prefetch_queryset(self, instances):
rel_obj_attr = attrgetter(self.related.field.attname)
instance_attr = lambda obj: obj._get_pk_val()
instances_dict = dict((instance_attr(inst), inst) for inst in instances)
params = {'%s__pk__in' % self.related.field.name: list(instances_dict)}
- qs = self.get_query_set(instance=instances[0]).filter(**params)
+ qs = self.get_queryset(instance=instances[0]).filter(**params)
# Since we're going to assign directly in the cache,
# we must manage the reverse relation cache manually.
rel_obj_cache_name = self.related.field.get_cache_name()
@@ -268,7 +276,7 @@ def __get__(self, instance, instance_type=None):
else:
params = {'%s__pk' % self.related.field.name: related_pk}
try:
- rel_obj = self.get_query_set(instance=instance).get(**params)
+ rel_obj = self.get_queryset(instance=instance).get(**params)
except self.related.model.DoesNotExist:
rel_obj = None
else:
@@ -321,7 +329,7 @@ def __set__(self, instance, value):
setattr(value, self.related.field.get_cache_name(), instance)
-class ReverseSingleRelatedObjectDescriptor(object):
+class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjectDescriptorMethods)):
# This class provides the functionality that makes the related-object
# managers available as attributes on a model class, for fields that have
# a single "remote" value, on the class that defines the related field.
@@ -334,7 +342,7 @@ def __init__(self, field_with_rel):
def is_cached(self, instance):
return hasattr(instance, self.cache_name)
- def get_query_set(self, **db_hints):
+ def get_queryset(self, **db_hints):
db = router.db_for_read(self.field.rel.to, **db_hints)
rel_mgr = self.field.rel.to._default_manager
# If the related manager indicates that it should be used for
@@ -344,7 +352,7 @@ def get_query_set(self, **db_hints):
else:
return QuerySet(self.field.rel.to).using(db)
- def get_prefetch_query_set(self, instances):
+ def get_prefetch_queryset(self, instances):
other_field = self.field.rel.get_related_field()
rel_obj_attr = attrgetter(other_field.attname)
instance_attr = attrgetter(self.field.attname)
@@ -353,7 +361,7 @@ def get_prefetch_query_set(self, instances):
params = {'%s__pk__in' % self.field.rel.field_name: list(instances_dict)}
else:
params = {'%s__in' % self.field.rel.field_name: list(instances_dict)}
- qs = self.get_query_set(instance=instances[0]).filter(**params)
+ qs = self.get_queryset(instance=instances[0]).filter(**params)
# Since we're going to assign directly in the cache,
# we must manage the reverse relation cache manually.
if not self.field.rel.multiple:
@@ -378,7 +386,7 @@ def __get__(self, instance, instance_type=None):
params = {'%s__%s' % (self.field.rel.field_name, other_field.rel.field_name): val}
else:
params = {'%s__exact' % self.field.rel.field_name: val}
- qs = self.get_query_set(instance=instance)
+ qs = self.get_queryset(instance=instance)
# Assuming the database enforces foreign keys, this won't fail.
rel_obj = qs.get(**params)
if not self.field.rel.multiple:
@@ -490,26 +498,26 @@ def __init__(self, instance):
}
self.model = rel_model
- def get_query_set(self):
+ def get_queryset(self):
try:
return self.instance._prefetched_objects_cache[rel_field.related_query_name()]
except (AttributeError, KeyError):
db = self._db or router.db_for_read(self.model, instance=self.instance)
- qs = super(RelatedManager, self).get_query_set().using(db).filter(**self.core_filters)
+ qs = super(RelatedManager, self).get_queryset().using(db).filter(**self.core_filters)
val = getattr(self.instance, attname)
if val is None or val == '' and connections[db].features.interprets_empty_strings_as_nulls:
return qs.none()
qs._known_related_objects = {rel_field: {self.instance.pk: self.instance}}
return qs
- def get_prefetch_query_set(self, instances):
+ def get_prefetch_queryset(self, instances):
rel_obj_attr = attrgetter(rel_field.attname)
instance_attr = attrgetter(attname)
instances_dict = dict((instance_attr(inst), inst) for inst in instances)
db = self._db or router.db_for_read(self.model, instance=instances[0])
query = {'%s__%s__in' % (rel_field.name, attname): list(instances_dict)}
- qs = super(RelatedManager, self).get_query_set().using(db).filter(**query)
- # Since we just bypassed this class' get_query_set(), we must manage
+ qs = super(RelatedManager, self).get_queryset().using(db).filter(**query)
+ # Since we just bypassed this class' get_queryset(), we must manage
# the reverse relation manually.
for rel_obj in qs:
instance = instances_dict[rel_obj_attr(rel_obj)]
@@ -603,20 +611,20 @@ def _get_fk_val(self, obj, field_name):
else:
return obj.pk
- def get_query_set(self):
+ def get_queryset(self):
try:
return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
except (AttributeError, KeyError):
db = self._db or router.db_for_read(self.instance.__class__, instance=self.instance)
- return super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**self.core_filters)
+ return super(ManyRelatedManager, self).get_queryset().using(db)._next_is_sticky().filter(**self.core_filters)
- def get_prefetch_query_set(self, instances):
+ def get_prefetch_queryset(self, instances):
instance = instances[0]
from django.db import connections
db = self._db or router.db_for_read(instance.__class__, instance=instance)
query = {'%s__pk__in' % self.query_field_name:
set(obj._get_pk_val() for obj in instances)}
- qs = super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**query)
+ qs = super(ManyRelatedManager, self).get_queryset().using(db)._next_is_sticky().filter(**query)
# M2M: need to annotate the query in order to get the primary model
# that the secondary model was actually related to. We know that
View
84 django/db/models/manager.py
@@ -3,7 +3,8 @@
from django.db.models.query import QuerySet, insert_query, RawQuerySet
from django.db.models import signals
from django.db.models.fields import FieldDoesNotExist
-
+from django.utils import six
+from django.utils.deprecation import RenameMethodsBase
def ensure_default_manager(sender, **kwargs):
"""
@@ -47,7 +48,14 @@ def ensure_default_manager(sender, **kwargs):
signals.class_prepared.connect(ensure_default_manager)
-class Manager(object):
+class RenameManagerMethods(RenameMethodsBase):
+ renamed_methods = (
+ ('get_query_set', 'get_queryset', PendingDeprecationWarning),
+ ('get_prefetch_query_set', 'get_prefetch_queryset', PendingDeprecationWarning),
+ )
+
+
+class Manager(six.with_metaclass(RenameManagerMethods)):
# Tracks each time a Manager instance is created. Used to retain order.
creation_counter = 0
@@ -112,113 +120,113 @@ def db(self):
# PROXIES TO QUERYSET #
#######################
- def get_query_set(self):
+ def get_queryset(self):
"""Returns a new QuerySet object. Subclasses can override this method
to easily customize the behavior of the Manager.
"""
return QuerySet(self.model, using=self._db)
def none(self):
- return self.get_query_set().none()
+ return self.get_queryset().none()
def all(self):
- return self.get_query_set()
+ return self.get_queryset()
def count(self):
- return self.get_query_set().count()
+ return self.get_queryset().count()
def dates(self, *args, **kwargs):
- return self.get_query_set().dates(*args, **kwargs)
+ return self.get_queryset().dates(*args, **kwargs)
def datetimes(self, *args, **kwargs):
- return self.get_query_set().datetimes(*args, **kwargs)
+ return self.get_queryset().datetimes(*args, **kwargs)
def distinct(self, *args, **kwargs):
- return self.get_query_set().distinct(*args, **kwargs)
+ return self.get_queryset().distinct(*args, **kwargs)
def extra(self, *args, **kwargs):
- return self.get_query_set().extra(*args, **kwargs)
+ return self.get_queryset().extra(*args, **kwargs)
def get(self, *args, **kwargs):
- return self.get_query_set().get(*args, **kwargs)
+ return self.get_queryset().get(*args, **kwargs)
def get_or_create(self, **kwargs):
- return self.get_query_set().get_or_create(**kwargs)
+ return self.get_queryset().get_or_create(**kwargs)
def create(self, **kwargs):
- return self.get_query_set().create(**kwargs)
+ return self.get_queryset().create(**kwargs)
def bulk_create(self, *args, **kwargs):
- return self.get_query_set().bulk_create(*args, **kwargs)
+ return self.get_queryset().bulk_create(*args, **kwargs)
def filter(self, *args, **kwargs):
- return self.get_query_set().filter(*args, **kwargs)
+ return self.get_queryset().filter(*args, **kwargs)
def aggregate(self, *args, **kwargs):
- return self.get_query_set().aggregate(*args, **kwargs)
+ return self.get_queryset().aggregate(*args, **kwargs)
def annotate(self, *args, **kwargs):
- return self.get_query_set().annotate(*args, **kwargs)
+ return self.get_queryset().annotate(*args, **kwargs)
def complex_filter(self, *args, **kwargs):
- return self.get_query_set().complex_filter(*args, **kwargs)
+ return self.get_queryset().complex_filter(*args, **kwargs)
def exclude(self, *args, **kwargs):
- return self.get_query_set().exclude(*args, **kwargs)
+ return self.get_queryset().exclude(*args, **kwargs)
def in_bulk(self, *args, **kwargs):
- return self.get_query_set().in_bulk(*args, **kwargs)
+ return self.get_queryset().in_bulk(*args, **kwargs)
def iterator(self, *args, **kwargs):
- return self.get_query_set().iterator(*args, **kwargs)
+ return self.get_queryset().iterator(*args, **kwargs)
def earliest(self, *args, **kwargs):
- return self.get_query_set().earliest(*args, **kwargs)
+ return self.get_queryset().earliest(*args, **kwargs)
def latest(self, *args, **kwargs):
- return self.get_query_set().latest(*args, **kwargs)
+ return self.get_queryset().latest(*args, **kwargs)
def order_by(self, *args, **kwargs):
- return self.get_query_set().order_by(*args, **kwargs)
+ return self.get_queryset().order_by(*args, **kwargs)
def select_for_update(self, *args, **kwargs):
- return self.get_query_set().select_for_update(*args, **kwargs)
+ return self.get_queryset().select_for_update(*args, **kwargs)
def select_related(self, *args, **kwargs):
- return self.get_query_set().select_related(*args, **kwargs)
+ return self.get_queryset().select_related(*args, **kwargs)
def prefetch_related(self, *args, **kwargs):
- return self.get_query_set().prefetch_related(*args, **kwargs)
+ return self.get_queryset().prefetch_related(*args, **kwargs)
def values(self, *args, **kwargs):
- return self.get_query_set().values(*args, **kwargs)
+ return self.get_queryset().values(*args, **kwargs)
def values_list(self, *args, **kwargs):
- return self.get_query_set().values_list(*args, **kwargs)
+ return self.get_queryset().values_list(*args, **kwargs)
def update(self, *args, **kwargs):
- return self.get_query_set().update(*args, **kwargs)
+ return self.get_queryset().update(*args, **kwargs)
def reverse(self, *args, **kwargs):
- return self.get_query_set().reverse(*args, **kwargs)
+ return self.get_queryset().reverse(*args, **kwargs)
def defer(self, *args, **kwargs):
- return self.get_query_set().defer(*args, **kwargs)
+ return self.get_queryset().defer(*args, **kwargs)
def only(self, *args, **kwargs):
- return self.get_query_set().only(*args, **kwargs)
+ return self.get_queryset().only(*args, **kwargs)
def using(self, *args, **kwargs):
- return self.get_query_set().using(*args, **kwargs)
+ return self.get_queryset().using(*args, **kwargs)
def exists(self, *args, **kwargs):
- return self.get_query_set().exists(*args, **kwargs)
+ return self.get_queryset().exists(*args, **kwargs)
def _insert(self, objs, fields, **kwargs):
return insert_query(self.model, objs, fields, **kwargs)
def _update(self, values, **kwargs):
- return self.get_query_set()._update(values, **kwargs)
+ return self.get_queryset()._update(values, **kwargs)
def raw(self, raw_query, params=None, *args, **kwargs):
return RawQuerySet(raw_query=raw_query, model=self.model, params=params, using=self._db, *args, **kwargs)
@@ -265,5 +273,5 @@ def __init__(self, model):
super(EmptyManager, self).__init__()
self.model = model
- def get_query_set(self):
- return super(EmptyManager, self).get_query_set().none()
+ def get_queryset(self):
+ return super(EmptyManager, self).get_queryset().none()
View
14 django/db/models/query.py
@@ -1733,9 +1733,9 @@ def prefetch_related_objects(result_cache, related_lookups):
def get_prefetcher(instance, attr):
"""
For the attribute 'attr' on the given instance, finds
- an object that has a get_prefetch_query_set().
+ an object that has a get_prefetch_queryset().
Returns a 4 tuple containing:
- (the object with get_prefetch_query_set (or None),
+ (the object with get_prefetch_queryset (or None),
the descriptor object representing this relationship (or None),
a boolean that is False if the attribute was not found at all,
a boolean that is True if the attribute has already been fetched)
@@ -1758,8 +1758,8 @@ def get_prefetcher(instance, attr):
attr_found = True
if rel_obj_descriptor:
# singly related object, descriptor object has the
- # get_prefetch_query_set() method.
- if hasattr(rel_obj_descriptor, 'get_prefetch_query_set'):
+ # get_prefetch_queryset() method.
+ if hasattr(rel_obj_descriptor, 'get_prefetch_queryset'):
prefetcher = rel_obj_descriptor
if rel_obj_descriptor.is_cached(instance):
is_fetched = True
@@ -1768,7 +1768,7 @@ def get_prefetcher(instance, attr):
# the attribute on the instance rather than the class to
# support many related managers
rel_obj = getattr(instance, attr)
- if hasattr(rel_obj, 'get_prefetch_query_set'):
+ if hasattr(rel_obj, 'get_prefetch_queryset'):
prefetcher = rel_obj
return prefetcher, rel_obj_descriptor, attr_found, is_fetched
@@ -1784,7 +1784,7 @@ def prefetch_one_level(instances, prefetcher, attname):
prefetches that must be done due to prefetch_related lookups
found from default managers.
"""
- # prefetcher must have a method get_prefetch_query_set() which takes a list
+ # prefetcher must have a method get_prefetch_queryset() which takes a list
# of instances, and returns a tuple:
# (queryset of instances of self.model that are related to passed in instances,
@@ -1797,7 +1797,7 @@ def prefetch_one_level(instances, prefetcher, attname):
# in a dictionary.
rel_qs, rel_obj_attr, instance_attr, single, cache_name =\
- prefetcher.get_prefetch_query_set(instances)
+ prefetcher.get_prefetch_queryset(instances)
# We have to handle the possibility that the default manager itself added
# prefetch_related lookups to the QuerySet we just got back. We don't want to
# trigger the prefetch_related functionality by evaluating the query.
View
6 django/forms/models.py
@@ -478,7 +478,7 @@ def get_queryset(self):
if self.queryset is not None:
qs = self.queryset
else:
- qs = self.model._default_manager.get_query_set()
+ qs = self.model._default_manager.get_queryset()
# If the queryset isn't already ordered we need to add an
# artificial ordering here to make sure that all formsets
@@ -668,9 +668,9 @@ def pk_is_not_editable(pk):
except IndexError:
pk_value = None
if isinstance(pk, OneToOneField) or isinstance(pk, ForeignKey):
- qs = pk.rel.to._default_manager.get_query_set()
+ qs = pk.rel.to._default_manager.get_queryset()
else:
- qs = self.model._default_manager.get_query_set()
+ qs = self.model._default_manager.get_queryset()
qs = qs.using(form.instance._state.db)
if form._meta.widgets:
widget = form._meta.widgets.get(self._pk_field.name, HiddenInput)
View
62 django/utils/deprecation.py
@@ -0,0 +1,62 @@
+import inspect
+import warnings
+
+
+class warn_about_renamed_method(object):
+ def __init__(self, class_name, old_method_name, new_method_name, deprecation_warning):
+ self.class_name = class_name
+ self.old_method_name = old_method_name
+ self.new_method_name = new_method_name
+ self.deprecation_warning = deprecation_warning
+
+ def __call__(self, f):
+ def wrapped(*args, **kwargs):
+ warnings.warn(
+ "`%s.%s` is deprecated, use `%s` instead." %
+ (self.class_name, self.old_method_name, self.new_method_name),
+ self.deprecation_warning, 2)
+ return f(*args, **kwargs)
+ return wrapped
+
+
+class RenameMethodsBase(type):
+ """
+ Handles the deprecation paths when renaming a method.
+
+ It does the following:
+ 1) Define the new method if missing and complain about it.
+ 2) Define the old method if missing.
+ 3) Complain whenever an old method is called.
+
+ See #15363 for more details.
+ """
+
+ renamed_methods = ()
+
+ def __new__(cls, name, bases, attrs):
+ new_class = super(RenameMethodsBase, cls).__new__(cls, name, bases, attrs)
+
+ for base in inspect.getmro(new_class):
+ class_name = base.__name__
+ for renamed_method in cls.renamed_methods:
+ old_method_name = renamed_method[0]
+ old_method = base.__dict__.get(old_method_name)
+ new_method_name = renamed_method[1]
+ new_method = base.__dict__.get(new_method_name)
+ deprecation_warning = renamed_method[2]
+ wrapper = warn_about_renamed_method(class_name, *renamed_method)
+
+ # Define the new method if missing and complain about it
+ if not new_method and old_method:
+ warnings.warn(
+ "`%s.%s` method should be renamed `%s`." %
+ (class_name, old_method_name, new_method_name),
+ deprecation_warning, 2)
+ setattr(base, new_method_name, old_method)
+ setattr(base, old_method_name, wrapper(old_method))
+
+ # Define the old method as a wrapped call to the new method.
+ if not old_method and new_method:
+ setattr(base, old_method_name, wrapper(new_method))
+
+ return new_class
View
2  docs/faq/admin.txt
@@ -49,7 +49,7 @@ How do I limit admin access so that objects can only be edited by the users who
The :class:`~django.contrib.admin.ModelAdmin` class also provides customization
hooks that allow you to control the visibility and editability of objects in the
admin. Using the same trick of extracting the user from the request, the
-:meth:`~django.contrib.admin.ModelAdmin.queryset` and
+:meth:`~django.contrib.admin.ModelAdmin.get_queryset` and
:meth:`~django.contrib.admin.ModelAdmin.has_change_permission` can be used to
control the visibility and editability of objects in the admin.
View
9 docs/internals/deprecation.txt
@@ -341,6 +341,15 @@ these changes.
* The private API ``django.db.close_connection`` will be removed.
+* Remove the backward compatible shims introduced to rename ``get_query_set``
+ and similar queryset methods. This affects the following classes:
+ ``BaseModelAdmin``, ``ChangeList``, ``BaseCommentNode``,
+ ``GenericForeignKey``, ``Manager``, ``SingleRelatedObjectDescriptor`` and
+ ``ReverseSingleRelatedObjectDescriptor``.
+
+* Remove the backward compatible shims introduced to rename the attributes
+ ``ChangeList.root_query_set`` and ``ChangeList.query_set``.
+
2.0
---
View
15 docs/ref/contrib/admin/index.txt
@@ -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`
View
18 docs/ref/models/querysets.txt
@@ -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
View
6 docs/releases/1.6.txt
@@ -289,3 +289,9 @@ on a widget, you should now define this method on the form field itself.
``Model._meta.module_name`` was renamed to ``model_name``. Despite being a
private API, it will go through a regular deprecation path.
+
+``get_query_set`` and similar methods renamed to ``get_queryset``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Methods that return a ``QuerySet`` such as ``Manager.get_query_set`` or
+``ModelAdmin.queryset`` have been renamed to ``get_queryset``.
View
25 docs/topics/db/managers.txt
@@ -108,7 +108,7 @@ example, using this model::
...the statement ``Book.objects.all()`` will return all books in the database.
You can override a ``Manager``\'s base ``QuerySet`` by overriding the
-``Manager.get_query_set()`` method. ``get_query_set()`` should return a
+``Manager.get_queryset()`` method. ``get_queryset()`` should return a
``QuerySet`` with the properties you require.
For example, the following model has *two* ``Manager``\s -- one that returns
@@ -116,8 +116,8 @@ all objects, and one that returns only the books by Roald Dahl::
# First, define the Manager subclass.
class DahlBookManager(models.Manager):
- def get_query_set(self):
- return super(DahlBookManager, self).get_query_set().filter(author='Roald Dahl')
+ def get_queryset(self):
+ return super(DahlBookManager, self).get_queryset().filter(author='Roald Dahl')
# Then hook it into the Book model explicitly.
class Book(models.Model):
@@ -131,7 +131,7 @@ With this sample model, ``Book.objects.all()`` will return all books in the
database, but ``Book.dahl_objects.all()`` will only return the ones written by
Roald Dahl.
-Of course, because ``get_query_set()`` returns a ``QuerySet`` object, you can
+Of course, because ``get_queryset()`` returns a ``QuerySet`` object, you can
use ``filter()``, ``exclude()`` and all the other ``QuerySet`` methods on it.
So these statements are all legal::
@@ -147,12 +147,12 @@ models.
For example::
class MaleManager(models.Manager):
- def get_query_set(self):
- return super(MaleManager, self).get_query_set().filter(sex='M')
+ def get_queryset(self):
+ return super(MaleManager, self).get_queryset().filter(sex='M')
class FemaleManager(models.Manager):
- def get_query_set(self):
- return super(FemaleManager, self).get_query_set().filter(sex='F')
+ def get_queryset(self):
+ return super(FemaleManager, self).get_queryset().filter(sex='F')
class Person(models.Model):
first_name = models.CharField(max_length=50)
@@ -172,9 +172,12 @@ the "default" ``Manager``, and several parts of Django
(including :djadmin:`dumpdata`) will use that ``Manager``
exclusively for that model. As a result, it's a good idea to be careful in
your choice of default manager in order to avoid a situation where overriding
-``get_query_set()`` results in an inability to retrieve objects you'd like to
+``get_queryset()`` results in an inability to retrieve objects you'd like to
work with.
+.. versionchanged:: 1.6
+ The ``get_queryset`` method was previously named ``get_query_set``.
+
.. _managers-for-related-objects:
Using managers for related object access
@@ -379,9 +382,9 @@ to from some other model. In those situations, Django has to be able to see
all the objects for the model it is fetching, so that *anything* which is
referred to can be retrieved.
-If you override the ``get_query_set()`` method and filter out any rows, Django
+If you override the ``get_queryset()`` method and filter out any rows, Django
will return incorrect results. Don't do that. A manager that filters results
-in ``get_query_set()`` is not appropriate for use as an automatic manager.
+in ``get_queryset()`` is not appropriate for use as an automatic manager.
Set ``use_for_related_fields`` when you define the class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
View
16 docs/topics/db/multi-db.txt
@@ -506,19 +506,19 @@ solution is to use ``db_manager()``, like this::
``db_manager()`` returns a copy of the manager bound to the database you specify.
-Using ``get_query_set()`` with multiple databases
+Using ``get_queryset()`` with multiple databases
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-If you're overriding ``get_query_set()`` on your manager, be sure to
+If you're overriding ``get_queryset()`` on your manager, be sure to
either call the method on the parent (using ``super()``) or do the
appropriate handling of the ``_db`` attribute on the manager (a string
containing the name of the database to use).
For example, if you want to return a custom ``QuerySet`` class from
-the ``get_query_set`` method, you could do this::
+the ``get_queryset`` method, you could do this::
class MyManager(models.Manager):
- def get_query_set(self):
+ def get_queryset(self):
qs = CustomQuerySet(self.model)
if self._db is not None:
qs = qs.using(self._db)
@@ -548,9 +548,9 @@ multiple-database support::
# Tell Django to delete objects from the 'other' database
obj.delete(using=self.using)
- def queryset(self, request):
+ def get_queryset(self, request):
# Tell Django to look for objects on the 'other' database.
- return super(MultiDBModelAdmin, self).queryset(request).using(self.using)
+ return super(MultiDBModelAdmin, self).get_queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
@@ -573,9 +573,9 @@ Inlines can be handled in a similar fashion. They require three customized metho
class MultiDBTabularInline(admin.TabularInline):
using = 'other'
- def queryset(self, request):
+ def get_queryset(self, request):
# Tell Django to look for inline objects on the 'other' database.
- return super(MultiDBTabularInline, self).queryset(request).using(self.using)
+ return super(MultiDBTabularInline, self).get_queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
View
8 tests/admin_changelist/admin.py
@@ -34,8 +34,8 @@ class ChildAdmin(admin.ModelAdmin):
list_per_page = 10
list_filter = ['parent', 'age']
- def queryset(self, request):
- return super(ChildAdmin, self).queryset(request).select_related("parent__name")
+ def get_queryset(self, request):
+ return super(ChildAdmin, self).get_queryset(request).select_related("parent__name")
class CustomPaginationAdmin(ChildAdmin):
@@ -46,8 +46,8 @@ class FilteredChildAdmin(admin.ModelAdmin):
list_display = ['name', 'parent']
list_per_page = 10
- def queryset(self, request):
- return super(FilteredChildAdmin, self).queryset(request).filter(
+ def get_queryset(self, request):
+ return super(FilteredChildAdmin, self).get_queryset(request).filter(
name__contains='filtered')
View
4 tests/admin_changelist/models.py
@@ -74,8 +74,8 @@ class UnorderedObject(models.Model):
class OrderedObjectManager(models.Manager):
- def get_query_set(self):
- return super(OrderedObjectManager, self).get_query_set().order_by('number')
+ def get_queryset(self):
+ return super(OrderedObjectManager, self).get_queryset().order_by('number')
class OrderedObject(models.Model):
"""
View
14 tests/admin_changelist/tests.py
@@ -39,15 +39,15 @@ def _mocked_authenticated_request(self, url, user):
def test_select_related_preserved(self):
"""
- Regression test for #10348: ChangeList.get_query_set() shouldn't
- overwrite a custom select_related provided by ModelAdmin.queryset().
+ Regression test for #10348: ChangeList.get_queryset() shouldn't
+ overwrite a custom select_related provided by ModelAdmin.get_queryset().
"""
m = ChildAdmin(Child, admin.site)
request = self.factory.get('/child/')
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)
- self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
+ self.assertEqual(cl.queryset.query.select_related, {'parent': {'name': {}}})
def test_result_list_empty_changelist_value(self):
"""
@@ -277,7 +277,7 @@ def test_distinct_for_non_unique_related_object_in_list_filter(self):
m.list_max_show_all, m.list_editable, m)
# Make sure distinct() was called
- self.assertEqual(cl.query_set.count(), 1)
+ self.assertEqual(cl.queryset.count(), 1)
def test_distinct_for_non_unique_related_object_in_search_fields(self):
"""
@@ -297,7 +297,7 @@ def test_distinct_for_non_unique_related_object_in_search_fields(self):
m.list_max_show_all, m.list_editable, m)
# Make sure distinct() was called
- self.assertEqual(cl.query_set.count(), 1)
+ self.assertEqual(cl.queryset.count(), 1)
def test_pagination(self):
"""
@@ -317,7 +317,7 @@ def test_pagination(self):
m.list_filter, m.date_hierarchy, m.search_fields,
m.list_select_related, m.list_per_page, m.list_max_show_all,
m.list_editable, m)
- self.assertEqual(cl.query_set.count(), 60)
+ self.assertEqual(cl.queryset.count(), 60)
self.assertEqual(cl.paginator.count, 60)
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3, 4, 5, 6])
@@ -327,7 +327,7 @@ def test_pagination(self):
m.list_filter, m.date_hierarchy, m.search_fields,
m.list_select_related, m.list_per_page, m.list_max_show_all,
m.list_editable, m)
- self.assertEqual(cl.query_set.count(), 30)
+ self.assertEqual(cl.queryset.count(), 30)
self.assertEqual(cl.paginator.count, 30)
self.assertEqual(list(cl.paginator.page_range), [1, 2, 3])
View
50 tests/admin_filters/tests.py
@@ -61,7 +61,7 @@ def queryset(self, request, queryset):
class DecadeListFilterWithQuerysetBasedLookups(DecadeListFilterWithTitleAndParameter):
def lookups(self, request, model_admin):
- qs = model_admin.queryset(request)
+ qs = model_admin.get_queryset(request)
if qs.filter(year__gte=1980, year__lte=1989).exists():
yield ('the 80s', "the 1980's")
if qs.filter(year__gte=1990, year__lte=1999).exists():
@@ -86,7 +86,7 @@ def lookups(self, request, model_admin):
return sorted(set([
(employee.department.id, # Intentionally not a string (Refs #19318)
employee.department.code)
- for employee in model_admin.queryset(request).all()
+ for employee in model_admin.get_queryset(request).all()
]))
def queryset(self, request, queryset):
@@ -183,7 +183,7 @@ def test_datefieldlistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book, self.djangonaut_book])
# Make sure the correct choice is selected
@@ -200,7 +200,7 @@ def test_datefieldlistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
if (self.today.year, self.today.month) == (self.one_week_ago.year, self.one_week_ago.month):
# In case one week ago is in the same month.
self.assertEqual(list(queryset), [self.gipsy_book, self.django_book, self.djangonaut_book])
@@ -221,7 +221,7 @@ def test_datefieldlistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
if self.today.year == self.one_week_ago.year:
# In case one week ago is in the same year.
self.assertEqual(list(queryset), [self.gipsy_book, self.django_book, self.djangonaut_book])
@@ -242,7 +242,7 @@ def test_datefieldlistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.gipsy_book, self.django_book, self.djangonaut_book])
# Make sure the correct choice is selected
@@ -266,7 +266,7 @@ def test_allvaluesfieldlistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book])
# Make sure the last choice is None and is selected
@@ -293,7 +293,7 @@ def test_relatedfieldlistfilter_foreignkey(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.gipsy_book])
# Make sure the last choice is None and is selected
@@ -321,7 +321,7 @@ def test_relatedfieldlistfilter_manytomany(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book, self.bio_book, self.djangonaut_book])
# Make sure the last choice is None and is selected
@@ -349,7 +349,7 @@ def test_relatedfieldlistfilter_reverse_relationships(self):
changelist = self.get_changelist(request, User, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.lisa])
# Make sure the last choice is None and is selected
@@ -374,7 +374,7 @@ def test_relatedfieldlistfilter_reverse_relationships(self):
changelist = self.get_changelist(request, User, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.alfred])
# Make sure the last choice is None and is selected
@@ -410,7 +410,7 @@ def verify_booleanfieldlistfilter(self, modeladmin):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.bio_book])
# Make sure the correct choice is selected
@@ -424,7 +424,7 @@ def verify_booleanfieldlistfilter(self, modeladmin):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.gipsy_book, self.djangonaut_book])
# Make sure the correct choice is selected
@@ -438,7 +438,7 @@ def verify_booleanfieldlistfilter(self, modeladmin):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book])
# Make sure the correct choice is selected
@@ -457,7 +457,7 @@ def test_simplelistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), list(Book.objects.all().order_by('-id')))
# Make sure the correct choice is selected
@@ -474,7 +474,7 @@ def test_simplelistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [])
# Make sure the correct choice is selected
@@ -491,7 +491,7 @@ def test_simplelistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.bio_book])
# Make sure the correct choice is selected
@@ -508,7 +508,7 @@ def test_simplelistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.gipsy_book, self.djangonaut_book])
# Make sure the correct choice is selected
@@ -525,7 +525,7 @@ def test_simplelistfilter(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.djangonaut_book])
# Make sure the correct choices are selected
@@ -615,7 +615,7 @@ def test_two_characters_long_field(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.bio_book])
filterspec = changelist.get_filters(request)[0][-1]
@@ -637,7 +637,7 @@ def test_parameter_ends_with__in__or__isnull(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.bio_book])
# Make sure the correct choice is selected
@@ -654,7 +654,7 @@ def test_parameter_ends_with__in__or__isnull(self):
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.bio_book])
# Make sure the correct choice is selected
@@ -676,7 +676,7 @@ def test_lookup_with_non_string_value(self):
request = self.request_factory.get('/', {'department': self.john.pk})
changelist = self.get_changelist(request, Employee, modeladmin)
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.john])
@@ -698,7 +698,7 @@ def test_fk_with_to_field(self):
changelist = self.get_changelist(request, Employee, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.jack, self.john])
filterspec = changelist.get_filters(request)[0][-1]
@@ -723,7 +723,7 @@ def test_fk_with_to_field(self):
changelist = self.get_changelist(request, Employee, modeladmin)
# Make sure the correct queryset is returned
- queryset = changelist.get_query_set(request)
+ queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.john])
filterspec = changelist.get_filters(request)[0][-1]
View
20 tests/admin_ordering/tests.py
@@ -22,8 +22,8 @@ def has_perm(self, perm):
class TestAdminOrdering(TestCase):
"""
- Let's make sure that ModelAdmin.queryset uses the ordering we define in
- ModelAdmin rather that ordering defined in the model's inner Meta
+ Let's make sure that ModelAdmin.get_queryset uses the ordering we define
+ in ModelAdmin rather that ordering defined in the model's inner Meta
class.
"""
@@ -42,7 +42,7 @@ def test_default_ordering(self):
class.
"""
ma = ModelAdmin(Band, None)
- names = [b.name for b in ma.queryset(request)]
+ names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Aerosmith', 'Radiohead', 'Van Halen'], names)
def test_specified_ordering(self):
@@ -53,7 +53,7 @@ def test_specified_ordering(self):
class BandAdmin(ModelAdmin):
ordering = ('rank',) # default ordering is ('name',)
ma = BandAdmin(Band, None)
- names = [b.name for b in ma.queryset(request)]
+ names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
def test_dynamic_ordering(self):
@@ -65,17 +65,17 @@ def test_dynamic_ordering(self):
request = self.request_factory.get('/')
request.user = super_user
ma = DynOrderingBandAdmin(Band, None)
- names = [b.name for b in ma.queryset(request)]
+ names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
request.user = other_user
- names = [b.name for b in ma.queryset(request)]
+ names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Aerosmith', 'Radiohead', 'Van Halen'], names)
class TestInlineModelAdminOrdering(TestCase):
"""
- Let's make sure that InlineModelAdmin.queryset uses the ordering we define
- in InlineModelAdmin.
+ Let's make sure that InlineModelAdmin.get_queryset uses the ordering we
+ define in InlineModelAdmin.
"""
def setUp(self):
@@ -95,7 +95,7 @@ def test_default_ordering(self):
class.
"""
inline = SongInlineDefaultOrdering(self.b, None)
- names = [s.name for s in inline.queryset(request)]
+ names = [s.name for s in inline.get_queryset(request)]
self.assertEqual(['Dude (Looks Like a Lady)', 'Jaded', 'Pink'], names)
def test_specified_ordering(self):
@@ -103,7 +103,7 @@ def test_specified_ordering(self):
Let's check with ordering set to something different than the default.
"""
inline = SongInlineNewOrdering(self.b, None)
- names = [s.name for s in inline.queryset(request)]
+ names = [s.name for s in inline.get_queryset(request)]
self.assertEqual(['Jaded', 'Pink', 'Dude (Looks Like a Lady)'], names)
View
36 tests/admin_views/admin.py
@@ -177,10 +177,10 @@ def get_changelist_formset(self, request, **kwargs):
return super(PersonAdmin, self).get_changelist_formset(request,
formset=BasePersonModelFormSet, **kwargs)
- def queryset(self, request):
+ def get_queryset(self, request):
# Order by a field that isn't in list display, to be able to test
# whether ordering is preserved.
- return super(PersonAdmin, self).queryset(request).order_by('age')
+ return super(PersonAdmin, self).get_queryset(request).order_by('age')
class FooAccount(Account):
@@ -283,8 +283,8 @@ def save_related(self, request, form, formsets, change):
class EmptyModelAdmin(admin.ModelAdmin):
- def queryset(self, request):
- return super(EmptyModelAdmin, self).queryset(request).filter(pk__gt=1)
+ def get_queryset(self, request):
+ return super(EmptyModelAdmin, self).get_queryset(request).filter(pk__gt=1)
class OldSubscriberAdmin(admin.ModelAdmin):
@@ -427,8 +427,8 @@ def multiline(self, instance):
class CustomChangeList(ChangeList):
- def get_query_set(self, request):
- return self.root_query_set.filter(pk=9999) # Does not exist
+ def get_queryset(self, request):
+ return self.root_queryset.filter(pk=9999) # Does not exist
class GadgetAdmin(admin.ModelAdmin):
@@ -452,52 +452,52 @@ class FoodDeliveryAdmin(admin.ModelAdmin):
class CoverLetterAdmin(admin.ModelAdmin):
"""
- A ModelAdmin with a custom queryset() method that uses defer(), to test
+ A ModelAdmin with a custom get_queryset() method that uses defer(), to test
verbose_name display in messages shown after adding/editing CoverLetter
instances.
Note that the CoverLetter model defines a __unicode__ method.
For testing fix for ticket #14529.
"""
- def queryset(self, request):
- return super(CoverLetterAdmin, self).queryset(request).defer('date_written')
+ def get_queryset(self, request):
+ return super(CoverLetterAdmin, self).get_queryset(request).defer('date_written')
class PaperAdmin(admin.ModelAdmin):
"""
- A ModelAdmin with a custom queryset() method that uses only(), to test
+ A ModelAdmin with a custom get_queryset() method that uses only(), to test
verbose_name display in messages shown after adding/editing Paper
instances.
For testing fix for ticket #14529.
"""
- def queryset(self, request):
- return super(PaperAdmin, self).queryset(request).only('title')
+ def get_queryset(self, request):
+ return super(PaperAdmin, self).get_queryset(request).only('title')
class ShortMessageAdmin(admin.ModelAdmin):
"""
- A ModelAdmin with a custom queryset() method that uses defer(), to test
+ A ModelAdmin with a custom get_queryset() method that uses defer(), to test
verbose_name display in messages shown after adding/editing ShortMessage
instances.
For testing fix for ticket #14529.
"""
- def queryset(self, request):
- return super(ShortMessageAdmin, self).queryset(request).defer('timestamp')
+ def get_queryset(self, request):
+ return super(ShortMessageAdmin, self).get_queryset(request).defer('timestamp')
class TelegramAdmin(admin.ModelAdmin):
"""
- A ModelAdmin with a custom queryset() method that uses only(), to test
+ A ModelAdmin with a custom get_queryset() method that uses only(), to test
verbose_name display in messages shown after adding/editing Telegram
instances.
Note that the Telegram model defines a __unicode__ method.
For testing fix for ticket #14529.
"""
- def queryset(self, request):
- return super(TelegramAdmin, self).queryset(request).only('title')
+ def get_queryset(self, request):
+ return super(TelegramAdmin, self).get_queryset(request).only('title')
class StoryForm(forms.ModelForm):
View
4 tests/admin_views/customadmin.py
@@ -35,8 +35,8 @@ def my_view(self, request):
class UserLimitedAdmin(UserAdmin):
# used for testing password change on a user not in queryset
- def queryset(self, request):
- qs = super(UserLimitedAdmin, self).queryset(request)
+ def get_queryset(self, request):
+ qs = super(UserLimitedAdmin, self).get_queryset(request)
return qs.filter(is_superuser=False)
View
12 tests/admin_views/tests.py
@@ -291,7 +291,7 @@ def testChangeListSortingPreserveQuerySetOrdering(self):
"""
If no ordering is defined in `ModelAdmin.ordering` or in the query
string, then the underlying order of the queryset should not be
- changed, even if it is defined in `Modeladmin.queryset()`.
+ changed, even if it is defined in `Modeladmin.get_queryset()`.
Refs #11868, #7309.
"""
p1 = Person.objects.create(name="Amy", gender=1, alive=True, age=80)
@@ -440,7 +440,7 @@ def testRelationSpanningFilters(self):
self.urlbit, query_string))
self.assertEqual(filtered_response.status_code, 200)
# ensure changelist contains only valid objects
- for obj in filtered_response.context['cl'].query_set.all():
+ for obj in filtered_response.context['cl'].queryset.all():
self.assertTrue(params['test'](obj, value))
def testIncorrectLookupParameters(self):
@@ -2583,7 +2583,7 @@ def test_change_view(self):
self.assertEqual(response.status_code, 404)
def test_add_model_modeladmin_defer_qs(self):
- # Test for #14529. defer() is used in ModelAdmin.queryset()
+ # Test for #14529. defer() is used in ModelAdmin.get_queryset()
# model has __unicode__ method
self.assertEqual(CoverLetter.objects.count(), 0)
@@ -2622,7 +2622,7 @@ def test_add_model_modeladmin_defer_qs(self):
)
def test_add_model_modeladmin_only_qs(self):
- # Test for #14529. only() is used in ModelAdmin.queryset()
+ # Test for #14529. only() is used in ModelAdmin.get_queryset()
# model has __unicode__ method
self.assertEqual(Telegram.objects.count(), 0)
@@ -2661,7 +2661,7 @@ def test_add_model_modeladmin_only_qs(self):
)
def test_edit_model_modeladmin_defer_qs(self):
- # Test for #14529. defer() is used in ModelAdmin.queryset()
+ # Test for #14529. defer() is used in ModelAdmin.get_queryset()
# model has __unicode__ method
cl = CoverLetter.objects.create(author="John Doe")
@@ -2708,7 +2708,7 @@ def test_edit_model_modeladmin_defer_qs(self):
)
def test_edit_model_modeladmin_only_qs(self):
- # Test for #14529. onl