Added object on SingleObjectMixin returned context, some code clanup.
This commit is contained in:
parent
0038296135
commit
3eba8c7f7f
|
@ -93,9 +93,11 @@ class SingleObjectMixin(ContextMixin):
|
||||||
Insert the single object into the context dict.
|
Insert the single object into the context dict.
|
||||||
"""
|
"""
|
||||||
context = {}
|
context = {}
|
||||||
context_object_name = self.get_context_object_name(self.object)
|
if self.object:
|
||||||
if context_object_name:
|
context['object'] = self.object
|
||||||
context[context_object_name] = self.object
|
context_object_name = self.get_context_object_name(self.object)
|
||||||
|
if context_object_name:
|
||||||
|
context[context_object_name] = self.object
|
||||||
context.update(kwargs)
|
context.update(kwargs)
|
||||||
return super(SingleObjectMixin, self).get_context_data(**context)
|
return super(SingleObjectMixin, self).get_context_data(**context)
|
||||||
|
|
||||||
|
|
|
@ -136,20 +136,6 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
|
||||||
self.object = form.save()
|
self.object = form.save()
|
||||||
return super(ModelFormMixin, self).form_valid(form)
|
return super(ModelFormMixin, self).form_valid(form)
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
|
||||||
"""
|
|
||||||
If an object has been supplied, inject it into the context with the
|
|
||||||
supplied context_object_name name.
|
|
||||||
"""
|
|
||||||
context = {}
|
|
||||||
if self.object:
|
|
||||||
context['object'] = self.object
|
|
||||||
context_object_name = self.get_context_object_name(self.object)
|
|
||||||
if context_object_name:
|
|
||||||
context[context_object_name] = self.object
|
|
||||||
context.update(kwargs)
|
|
||||||
return super(ModelFormMixin, self).get_context_data(**context)
|
|
||||||
|
|
||||||
|
|
||||||
class ProcessFormView(View):
|
class ProcessFormView(View):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -412,6 +412,19 @@ class GetContextDataTest(unittest.TestCase):
|
||||||
context = test_view.get_context_data(test_name='test_value')
|
context = test_view.get_context_data(test_name='test_value')
|
||||||
self.assertEqual(context['test_name'], 'test_value')
|
self.assertEqual(context['test_name'], 'test_value')
|
||||||
|
|
||||||
|
def test_object_at_custom_name_in_context_data(self):
|
||||||
|
# Checks 'pony' key presence in dict returned by get_context_date
|
||||||
|
test_view = views.CustomSingleObjectView()
|
||||||
|
test_view.context_object_name = 'pony'
|
||||||
|
context = test_view.get_context_data()
|
||||||
|
self.assertEqual(context['pony'], test_view.object)
|
||||||
|
|
||||||
|
def test_object_in_get_context_data(self):
|
||||||
|
# Checks 'object' key presence in dict returned by get_context_date #20234
|
||||||
|
test_view = views.CustomSingleObjectView()
|
||||||
|
context = test_view.get_context_data()
|
||||||
|
self.assertEqual(context['object'], test_view.object)
|
||||||
|
|
||||||
|
|
||||||
class UseMultipleObjectMixinTest(unittest.TestCase):
|
class UseMultipleObjectMixinTest(unittest.TestCase):
|
||||||
rf = RequestFactory()
|
rf = RequestFactory()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.contrib.messages.views import SuccessMessageMixin
|
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
from django.core.urlresolvers import reverse, reverse_lazy
|
from django.core.urlresolvers import reverse, reverse_lazy
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
|
@ -227,6 +226,10 @@ class CustomContextView(generic.detail.SingleObjectMixin, generic.View):
|
||||||
def get_context_object_name(self, obj):
|
def get_context_object_name(self, obj):
|
||||||
return "test_name"
|
return "test_name"
|
||||||
|
|
||||||
|
class CustomSingleObjectView(generic.detail.SingleObjectMixin, generic.View):
|
||||||
|
model = Book
|
||||||
|
object = Book(name="dummy")
|
||||||
|
|
||||||
class BookSigningConfig(object):
|
class BookSigningConfig(object):
|
||||||
model = BookSigning
|
model = BookSigning
|
||||||
date_field = 'event_date'
|
date_field = 'event_date'
|
||||||
|
|
Loading…
Reference in New Issue