Ticket #16502: SingleObjectMixin-get_model.diff

File SingleObjectMixin-get_model.diff, 3.4 KB (added by bhuztez, 13 years ago)

add a get_model to SingleObjectMixin?

  • django/views/generic/detail.py

    diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
    index 7e9047f..43ec36e 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, **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):
Back to Top