1 | class BaseCreateUpdateView(ModelFormMixin, ProcessFormView):
|
---|
2 | """
|
---|
3 | Base view for creating and updating an existing object.
|
---|
4 |
|
---|
5 | Using this base class requires subclassing to provide a response mixin.
|
---|
6 | """
|
---|
7 |
|
---|
8 | object_url_kwargs = ['slug', 'pk']
|
---|
9 |
|
---|
10 | def get_object(self, queryset=None):
|
---|
11 | if self.is_update_request():
|
---|
12 | return super(BaseCreateUpdateView, self).get_object(queryset)
|
---|
13 | else:
|
---|
14 | return None
|
---|
15 |
|
---|
16 | def is_update_request(self):
|
---|
17 | """
|
---|
18 | Returns True if current request is an object update request, False if it's an object create request.
|
---|
19 |
|
---|
20 | Checks if the URL contains a parameter identifying an object.
|
---|
21 | Possible URL parameter names are defined in self.object_url_kwargs
|
---|
22 | """
|
---|
23 | for object_kwarg in self.object_url_kwargs:
|
---|
24 | if object_kwarg in self.kwargs:
|
---|
25 | return True
|
---|
26 | return False
|
---|
27 |
|
---|
28 | def get(self, request, *args, **kwargs):
|
---|
29 | self.object = self.get_object()
|
---|
30 | return super(BaseCreateUpdateView, self).get(request, *args, **kwargs)
|
---|
31 |
|
---|
32 | def post(self, request, *args, **kwargs):
|
---|
33 | self.object = self.get_object()
|
---|
34 | return super(BaseCreateUpdateView, self).post(request, *args, **kwargs)
|
---|
35 |
|
---|
36 |
|
---|
37 | class CreateUpdateView(SingleObjectTemplateResponseMixin, BaseCreateUpdateView):
|
---|
38 | """
|
---|
39 | View for creating a new object instance or updating one,
|
---|
40 | with a response rendered by template.
|
---|
41 | """
|
---|
42 | template_name_suffix = '_form'
|
---|