Ticket #16502: get_model_with_tests.2.diff

File get_model_with_tests.2.diff, 6.7 KB (added by bhuztez, 13 years ago)

update patch for revision 17517

  • django/views/generic/detail.py

    diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
    index b9278bb..f6ff844 100644
    a b class SingleObjectMixin(object):  
    5858        `get_object` is overridden.
    5959        """
    6060        if self.queryset is None:
    61             if self.model:
    62                 return self.model._default_manager.all()
     61            model = self.get_model()
     62            if model:
     63                return model._default_manager.all()
    6364            else:
    6465                raise ImproperlyConfigured(u"%(cls)s is missing a queryset. Define "
    6566                                           u"%(cls)s.model, %(cls)s.queryset, or override "
    class SingleObjectMixin(object):  
    9293            context[context_object_name] = self.object
    9394        return context
    9495
     96    def get_model(self):
     97        return self.model
     98
    9599
    96100class BaseDetailView(SingleObjectMixin, View):
    97101    def get(self, request, *args, **kwargs):
    class SingleObjectTemplateResponseMixin(TemplateResponseMixin):  
    132136                self.object._meta.object_name.lower(),
    133137                self.template_name_suffix
    134138            ))
    135         elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
    136             names.append("%s/%s%s.html" % (
    137                 self.model._meta.app_label,
    138                 self.model._meta.object_name.lower(),
    139                 self.template_name_suffix
    140             ))
     139        elif hasattr(self, 'get_model'):
     140            model = self.get_model()
     141            if hasattr(model, '_meta'):
     142                names.append("%s/%s%s.html" % (
     143                    model._meta.app_label,
     144                    model._meta.object_name.lower(),
     145                    self.template_name_suffix
     146                ))
    141147        return names
    142148
    143149
  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    index 3cade52..fbe09b9 100644
    a b class ModelFormMixin(FormMixin, SingleObjectMixin):  
    7575        if self.form_class:
    7676            return self.form_class
    7777        else:
    78             if self.model is not None:
    79                 # If a model has been explicitly provided, use it
    80                 model = self.model
    81             elif hasattr(self, 'object') and self.object is not None:
    82                 # If this view is operating on a single object, use
    83                 # the class of that object
    84                 model = self.object.__class__
    85             else:
    86                 # Try to get a queryset and extract the model class
    87                 # from that
    88                 model = self.get_queryset().model
     78            model = self.get_model()
    8979            return model_forms.modelform_factory(model)
    9080
    9181    def get_form_kwargs(self):
    class ModelFormMixin(FormMixin, SingleObjectMixin):  
    120110            if context_object_name:
    121111                context[context_object_name] = self.object
    122112        return context
     113       
     114    def get_model(self):
     115        if self.model:
     116            return self.model
     117        elif self.form_class and issubclass(self.form_class, model_forms.ModelForm):
     118            return self.form_class._meta.model
     119        elif hasattr(self, 'object') and self.object is not None:
     120            return self.object.__class__
     121        else:
     122            return self.get_queryset().model
    123123
    124124
    125125class ProcessFormView(View):
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 182615a..4c63c5b 100644
    a b class CreateViewTests(TestCase):  
    7878        self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
    7979        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
    8080
     81    def test_create_with_only_form_class(self):
     82        res = self.client.get('/edit/artists/create/formclass/')
     83        self.assertEqual(res.status_code, 200)
     84        self.assertTrue(isinstance(res.context['form'], views.ArtistForm))
     85        self.assertFalse('object' in res.context)
     86        self.assertFalse('artist' in res.context)
     87        self.assertTemplateUsed(res, 'generic_views/artist_form.html')
     88       
     89        res = self.client.post('/edit/artists/create/formclass/',
     90                               {'name': 'Rene Magritte'})
     91        self.assertEqual(res.status_code, 302)
     92        artist = Artist.objects.get(name='Rene Magritte')
     93        self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % artist.pk)
     94        self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
     95
    8196    def test_create_without_redirect(self):
    8297        try:
    8398            res = self.client.post('/edit/authors/create/naive/',
  • tests/regressiontests/generic_views/forms.py

    diff --git a/tests/regressiontests/generic_views/forms.py b/tests/regressiontests/generic_views/forms.py
    index a78242f..3d7079e 100644
    a b from __future__ import absolute_import  
    22
    33from django import forms
    44
    5 from .models import Author
     5from .models import Author, Artist
    66
    77
    88class AuthorForm(forms.ModelForm):
    class AuthorForm(forms.ModelForm):  
    1111
    1212    class Meta:
    1313        model = Author
     14
     15
     16class ArtistForm(forms.ModelForm):
     17    name = forms.CharField()
     18
     19    class Meta:
     20        model = Artist
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index 090ec73..93c49d4 100644
    a b urlpatterns = patterns('',  
    5656    # Create/UpdateView
    5757    (r'^edit/artists/create/$',
    5858        views.ArtistCreate.as_view()),
     59    (r'^edit/artists/create/formclass/$',
     60        views.ArtistOnlyFormClassCreate.as_view()),
    5961    (r'^edit/artists/(?P<pk>\d+)/update/$',
    6062        views.ArtistUpdate.as_view()),
    6163
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    index 5ff9cf0..a7b4238 100644
    a b from django.core.urlresolvers import reverse  
    66from django.utils.decorators import method_decorator
    77from django.views import generic
    88
    9 from .forms import AuthorForm
     9from .forms import AuthorForm, ArtistForm
    1010from .models import Artist, Author, Book, Page
    1111
    1212
    class AuthorListCustomPaginator(AuthorList):  
    7979class ArtistCreate(generic.CreateView):
    8080    model = Artist
    8181
     82class ArtistOnlyFormClassCreate(generic.CreateView):
     83    form_class = ArtistForm
     84
    8285
    8386class NaiveAuthorCreate(generic.CreateView):
    8487    queryset = Author.objects.all()
Back to Top