magic-removal: Updated list_detail generic views to take 'queryset' instead of 'model'
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2274 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
00752157e4
commit
fae0dde7bb
|
@ -4,8 +4,8 @@ from django.core.xheaders import populate_xheaders
|
||||||
from django.core.paginator import ObjectPaginator, InvalidPage
|
from django.core.paginator import ObjectPaginator, InvalidPage
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
||||||
def object_list(request, model, paginate_by=None, allow_empty=False,
|
def object_list(request, queryset, paginate_by=None, allow_empty=False,
|
||||||
template_name=None, template_loader=loader, extra_lookup_kwargs={},
|
template_name=None, template_loader=loader,
|
||||||
extra_context={}, context_processors=None):
|
extra_context={}, context_processors=None):
|
||||||
"""
|
"""
|
||||||
Generic list of objects.
|
Generic list of objects.
|
||||||
|
@ -33,9 +33,10 @@ def object_list(request, model, paginate_by=None, allow_empty=False,
|
||||||
hits
|
hits
|
||||||
number of objects, total
|
number of objects, total
|
||||||
"""
|
"""
|
||||||
lookup_kwargs = extra_lookup_kwargs.copy()
|
queryset = queryset._clone()
|
||||||
|
model = queryset.model
|
||||||
if paginate_by:
|
if paginate_by:
|
||||||
paginator = ObjectPaginator(model, lookup_kwargs, paginate_by)
|
paginator = ObjectPaginator(queryset, paginate_by)
|
||||||
page = request.GET.get('page', 0)
|
page = request.GET.get('page', 0)
|
||||||
try:
|
try:
|
||||||
object_list = paginator.get_page(page)
|
object_list = paginator.get_page(page)
|
||||||
|
@ -58,12 +59,11 @@ def object_list(request, model, paginate_by=None, allow_empty=False,
|
||||||
'hits' : paginator.hits,
|
'hits' : paginator.hits,
|
||||||
}, context_processors)
|
}, context_processors)
|
||||||
else:
|
else:
|
||||||
object_list = model._default_manager.filter(**lookup_kwargs)
|
|
||||||
c = RequestContext(request, {
|
c = RequestContext(request, {
|
||||||
'object_list': object_list,
|
'object_list': queryset,
|
||||||
'is_paginated': False
|
'is_paginated': False
|
||||||
}, context_processors)
|
}, context_processors)
|
||||||
if len(object_list) == 0 and not allow_empty:
|
if not allow_empty and len(queryset) == 0:
|
||||||
raise Http404
|
raise Http404
|
||||||
for key, value in extra_context.items():
|
for key, value in extra_context.items():
|
||||||
if callable(value):
|
if callable(value):
|
||||||
|
@ -75,9 +75,9 @@ def object_list(request, model, paginate_by=None, allow_empty=False,
|
||||||
t = template_loader.get_template(template_name)
|
t = template_loader.get_template(template_name)
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
def object_detail(request, model, object_id=None, slug=None,
|
def object_detail(request, queryset, object_id=None, slug=None,
|
||||||
slug_field=None, template_name=None, template_name_field=None,
|
slug_field=None, template_name=None, template_name_field=None,
|
||||||
template_loader=loader, extra_lookup_kwargs={}, extra_context={},
|
template_loader=loader, extra_context={},
|
||||||
context_processors=None):
|
context_processors=None):
|
||||||
"""
|
"""
|
||||||
Generic list of objects.
|
Generic list of objects.
|
||||||
|
@ -87,18 +87,17 @@ def object_detail(request, model, object_id=None, slug=None,
|
||||||
object
|
object
|
||||||
the object
|
the object
|
||||||
"""
|
"""
|
||||||
lookup_kwargs = {}
|
model = queryset.model
|
||||||
if object_id:
|
if object_id:
|
||||||
lookup_kwargs['pk'] = object_id
|
queryset = queryset.filter(pk=object_id)
|
||||||
elif slug and slug_field:
|
elif slug and slug_field:
|
||||||
lookup_kwargs['%s__exact' % slug_field] = slug
|
queryset = queryset.filter(**{slug_field: slug})
|
||||||
else:
|
else:
|
||||||
raise AttributeError, "Generic detail view must be called with either an object_id or a slug/slug_field."
|
raise AttributeError, "Generic detail view must be called with either an object_id or a slug/slug_field."
|
||||||
lookup_kwargs.update(extra_lookup_kwargs)
|
|
||||||
try:
|
try:
|
||||||
object = model._default_manager.get(**lookup_kwargs)
|
object = queryset.get()
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
raise Http404, "No %s found for %s" % (model._meta.verbose_name, lookup_kwargs)
|
raise Http404, "No %s found matching the query" % (model._meta.verbose_name)
|
||||||
if not template_name:
|
if not template_name:
|
||||||
template_name = "%s/%s_detail" % (model._meta.app_label, model._meta.object_name.lower())
|
template_name = "%s/%s_detail" % (model._meta.app_label, model._meta.object_name.lower())
|
||||||
if template_name_field:
|
if template_name_field:
|
||||||
|
|
Loading…
Reference in New Issue