Code

Ticket #16502: SingleObjectMixin-get_model.diff

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

add a get_model to SingleObjectMixin?

Line 
1diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
2index 7e9047f..43ec36e 100644
3--- a/django/views/generic/detail.py
4+++ b/django/views/generic/detail.py
5@@ -58,8 +58,9 @@ class SingleObjectMixin(object):
6         `get_object` is overridden.
7         """
8         if self.queryset is None:
9-            if self.model:
10-                return self.model._default_manager.all()
11+            model = self.get_model()
12+            if model:
13+                return model._default_manager.all()
14             else:
15                 raise ImproperlyConfigured(u"%(cls)s is missing a queryset. Define "
16                                            u"%(cls)s.model, %(cls)s.queryset, or override "
17@@ -92,6 +93,9 @@ class SingleObjectMixin(object):
18             context[context_object_name] = self.object
19         return context
20 
21+    def get_model(self):
22+        return self.model
23+
24 
25 class BaseDetailView(SingleObjectMixin, View):
26     def get(self, request, **kwargs):
27@@ -132,12 +136,14 @@ class SingleObjectTemplateResponseMixin(TemplateResponseMixin):
28                 self.object._meta.object_name.lower(),
29                 self.template_name_suffix
30             ))
31-        elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
32-            names.append("%s/%s%s.html" % (
33-                self.model._meta.app_label,
34-                self.model._meta.object_name.lower(),
35-                self.template_name_suffix
36-            ))
37+        elif hasattr(self, 'get_model'):
38+            model = self.get_model()
39+            if hasattr(model, '_meta'):
40+                names.append("%s/%s%s.html" % (
41+                    model._meta.app_label,
42+                    model._meta.object_name.lower(),
43+                    self.template_name_suffix
44+                ))
45         return names
46 
47 
48diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
49index 3cade52..fbe09b9 100644
50--- a/django/views/generic/edit.py
51+++ b/django/views/generic/edit.py
52@@ -75,17 +75,7 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
53         if self.form_class:
54             return self.form_class
55         else:
56-            if self.model is not None:
57-                # If a model has been explicitly provided, use it
58-                model = self.model
59-            elif hasattr(self, 'object') and self.object is not None:
60-                # If this view is operating on a single object, use
61-                # the class of that object
62-                model = self.object.__class__
63-            else:
64-                # Try to get a queryset and extract the model class
65-                # from that
66-                model = self.get_queryset().model
67+            model = self.get_model()
68             return model_forms.modelform_factory(model)
69 
70     def get_form_kwargs(self):
71@@ -120,6 +110,16 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
72             if context_object_name:
73                 context[context_object_name] = self.object
74         return context
75+       
76+    def get_model(self):
77+        if self.model:
78+            return self.model
79+        elif self.form_class and issubclass(self.form_class, model_forms.ModelForm):
80+            return self.form_class._meta.model
81+        elif hasattr(self, 'object') and self.object is not None:
82+            return self.object.__class__
83+        else:
84+            return self.get_queryset().model
85 
86 
87 class ProcessFormView(View):