1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

Modified the implementation of get_object() to be consistent with the approach used elsewhere in the API. Also added documentation for get_object() which seems to have been accidentally omitted.

This is a BACKWARDS-INCOMPATIBLE CHANGE for anyone depending on the API for get_object() that was introduced (but not documented) in r14254.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14292 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-10-20 00:21:47 +00:00
parent 46c17654ed
commit a014ee0288
5 changed files with 28 additions and 13 deletions

View File

@ -443,7 +443,7 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV
Detail view of a single object on a single date; this differs from the Detail view of a single object on a single date; this differs from the
standard DetailView by accepting a year/month/day in the URL. standard DetailView by accepting a year/month/day in the URL.
""" """
def get_object(self, queryset=None, **kwargs): def get_object(self, queryset=None):
""" """
Get the object this request displays. Get the object this request displays.
""" """
@ -469,7 +469,7 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV
lookup = _date_lookup_for_field(field, date) lookup = _date_lookup_for_field(field, date)
qs = qs.filter(**lookup) qs = qs.filter(**lookup)
return super(BaseDetailView, self).get_object(queryset=qs, **kwargs) return super(BaseDetailView, self).get_object(queryset=qs)

View File

@ -14,7 +14,7 @@ class SingleObjectMixin(object):
slug_field = 'slug' slug_field = 'slug'
context_object_name = None context_object_name = None
def get_object(self, pk=None, slug=None, queryset=None, **kwargs): def get_object(self, queryset=None):
""" """
Returns the object the view is displaying. Returns the object the view is displaying.
@ -27,6 +27,8 @@ class SingleObjectMixin(object):
queryset = self.get_queryset() queryset = self.get_queryset()
# Next, try looking up by primary key. # Next, try looking up by primary key.
pk = self.kwargs.get('pk', None)
slug = self.kwargs.get('slug', None)
if pk is not None: if pk is not None:
queryset = queryset.filter(pk=pk) queryset = queryset.filter(pk=pk)
@ -92,7 +94,7 @@ class SingleObjectMixin(object):
class BaseDetailView(SingleObjectMixin, View): class BaseDetailView(SingleObjectMixin, View):
def get(self, request, **kwargs): def get(self, request, **kwargs):
self.object = self.get_object(**kwargs) self.object = self.get_object()
context = self.get_context_data(object=self.object) context = self.get_context_data(object=self.object)
return self.render_to_response(context) return self.render_to_response(context)

View File

@ -2,8 +2,8 @@ from django.forms import models as model_forms
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.views.generic.base import TemplateResponseMixin, View from django.views.generic.base import TemplateResponseMixin, View
from django.views.generic.detail import (SingleObjectMixin, from django.views.generic.detail import (SingleObjectMixin,
SingleObjectTemplateResponseMixin, BaseDetailView) SingleObjectTemplateResponseMixin, BaseDetailView)
class FormMixin(object): class FormMixin(object):
@ -191,11 +191,11 @@ class BaseUpdateView(ModelFormMixin, ProcessFormView):
Using this base class requires subclassing to provide a response mixin. Using this base class requires subclassing to provide a response mixin.
""" """
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.object = self.get_object(**kwargs) self.object = self.get_object()
return super(BaseUpdateView, self).get(request, *args, **kwargs) return super(BaseUpdateView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
self.object = self.get_object(**kwargs) self.object = self.get_object()
return super(BaseUpdateView, self).post(request, *args, **kwargs) return super(BaseUpdateView, self).post(request, *args, **kwargs)
# PUT is a valid HTTP verb for creating (with a known URL) or editing an # PUT is a valid HTTP verb for creating (with a known URL) or editing an
@ -218,7 +218,7 @@ class DeletionMixin(object):
success_url = None success_url = None
def delete(self, request, *args, **kwargs): def delete(self, request, *args, **kwargs):
self.object = self.get_object(**kwargs) self.object = self.get_object()
self.object.delete() self.object.delete()
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())

View File

@ -158,6 +158,19 @@ SingleObjectMixin
Designates the name of the variable to use in the context. Designates the name of the variable to use in the context.
.. method:: get_object(queryset=None)
Returns the single object that this view will display. If
``queryset`` is provided, that queryset will be used as the
source of objects; otherwise,
:meth:`~SingleObjectMixin.get_queryset` will be used.
:meth:`~SingleObjectMixin.get_object` looks for a ``pk``
argument in the arguments to the view; if ``pk`` is found,
this method performs a primary-key based lookup using that
value. If no ``pk`` argument is found, it looks for a ``slug``
argument, and performs a slug lookup using the
:attr:`SingleObjectMixin.slug_field`.
.. method:: get_queryset() .. method:: get_queryset()
Returns the queryset that will be used to retrieve the object that Returns the queryset that will be used to retrieve the object that
@ -311,7 +324,7 @@ MultipleObjectMixin
objects from that page. objects from that page.
.. method:: get_paginate_by(queryset) .. method:: get_paginate_by(queryset)
Returns the number of items to paginate by, or ``None`` for no Returns the number of items to paginate by, or ``None`` for no
pagination. By default this simply returns the value of pagination. By default this simply returns the value of
:attr:`MultipleObjectMixin.paginate_by`. :attr:`MultipleObjectMixin.paginate_by`.
@ -506,7 +519,7 @@ ProcessFormView
A mixin that provides basic HTTP GET and POST workflow. A mixin that provides basic HTTP GET and POST workflow.
.. method:: get(request, *args, **kwargs) .. method:: get(request, *args, **kwargs)
Constructs a form, then renders a response using a context that Constructs a form, then renders a response using a context that
contains that form. contains that form.

View File

@ -440,9 +440,9 @@ object, so we simply override it and wrap the call::
queryset = Author.objects.all() queryset = Author.objects.all()
def get_object(self, **kwargs): def get_object(self):
# Call the superclass # Call the superclass
object = super(AuthorDetailView, self).get_object(**kwargs) object = super(AuthorDetailView, self).get_object()
# Record the lass accessed date # Record the lass accessed date
object.last_accessed = datetime.datetime.now() object.last_accessed = datetime.datetime.now()
object.save() object.save()