8/12/2023 0 Comments Wagtail django rest framework![]() The views using AuditableMixin should allow only logged in users (or else anĮxception will be thrown). Is required (save the model instance and redirect to success_url by default). Finally, it will call the next form_valid method to do whatever After that it will set the modified_by attribute of the object to The above mixin overrides the form_valid method of CreateView and UpdateView:įirst it checks if the object is created (if it is created it won’t be saved in theĭatabase yet thus it won’t have an id) in order to set the created_by attribute to user return super ( AuditableMixin, self ). Since I am really fond of CBVs I will present a simple mixin that can be used with CreateViewĬlass AuditableMixin ( object ,): def form_valid ( self, form, ): if not form. Is that they cannot be filled automatically (like the datetimes) because the user thatĪctually did create/change the objects must be provided! That created the and one of the user that modified them. Such models will also have two foreign keys to User, one for the user Set the current datetime when the model instance is modified). Set the current datetime when the model instance is created) and auto_now_ (which will Which will be automatically filled using the very usefull auto_now_add_ (which will Models inheriting from Auditable will contain their datetime of creation and modification AUTH_USER_MODEL, related_name = 'modified_by' ) class Meta : abstract = True DateTimeField ( auto_now = True ) modified_by = models. ![]() AUTH_USER_MODEL, related_name = 'created_by' ) modified_on = models. DateTimeField ( auto_now_add = True ) created_by = models. From nf import settings from django.db import models class Auditable ( models.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |