viewhelpers.formview.create_update_view_mixin — Create and update view mixins¶
-
class
CreateUpdateViewMixin
¶ Bases:
django_cradmin.viewhelpers.formview.previewmixin.PreviewMixin
,django_cradmin.viewhelpers.formview.formviewmixin.FormViewMixin
Mixin class for Update and Create views.
Note
You should import this class with
from django_cradmin import viewhelpers
, and refer to it usingviewhelpers.formview.CreateUpdateViewMixin
.-
model
= None¶ The model class.
-
fields
= None¶ List of field names. See
get_model_fields()
andget_field_layout()
.
-
roleid_field
= None¶ The field that should always be set to the current role. Removes the field from the form (see
get_form()
), and instead sets the value directly on the object insave_object()
.
-
get_form
(form_class=None)¶ If you set
roleid_field
, we will remove that field from the form.Note
The
roleid_field
handling also works for GenericForeignKey fields (removes the content type and object pk field from the form).
-
model_verbose_name
¶ Get the verbose name of the model.
-
add_create_update_view_mixin_context_data
(context)¶ Must be called in
get_context_data()
in subclasses to add the required context data.Parameters: context – The template context data.
-
set_automatic_attributes
(obj, form)¶ Called by
save_object()
to set automatic attributes for the object before it is saved.This is where we handle
roleid_field
, but you can override this to set your own automatic attributes. Just remember to callsuper
if you want to keep theroleid_field
magic.Parameters: - obj – The object you are about to save.
- form – The cleaned form.
-
save_object
(form, commit=True)¶ Save the object. You can override this to customize how the form is turned into a saved object.
Make sure you call
super
if you override this (see the docs for the commit parameter). If you do not, you will loose the automatic handling of obj:.roleid_field.Parameters: commit (boolean) – If this is False
, the object is returned unsaved. Very useful when you want to manipulate the object before saving it in a subclass.Returns: The saved object.
-
form_valid
(form)¶ If the form is valid, save the associated model.
-
form_saved
(object)¶ Called after the form has been successfully saved. The
object
is the saved object.Does nothing by default, but you can override it if you need to do something extra post save.
-
get_success_message
(obj)¶ Override this to provide a success message.
The
obj
is the saved object.Used by
add_success_messages()
.
-
add_success_messages
(obj)¶ Called after the form has been saved, and after
form_saved()
has been called.The
obj
is the saved obj.Defaults to add
get_success_message()
as a django messages success message ifget_success_message()
returns anything.You can override this to add multiple messages or to show messages in some other way.
-
serialize_preview
(form)¶ Seralize for preview.
Defaults to serializing the object as JSON using
django.core.serializers
. You can safely override this, but you will also have to overridedeserialize_preview()
.
-
classmethod
deserialize_preview
(serialized)¶ Deseralize a preview serialized with
serialize_preview()
.You must override this and
serialize_preview()
- they work together to send the preview to the preview View.
-
classmethod
get_preview_sessionkey
()¶ Get the session key used for preview. You should not need to override this.
Unlike the default implementation of this method from
django_cradmin.viewhelpers.formbase.PreviewMixin
, we use the model class module and name as the session key. This is simply because we do not want it to matter if you fetch preview data from create or update views for the same model (to simplify implementing preview views).
-