Created
July 21, 2016 09:52
-
-
Save luanvuhlu/0ced713c3fad2668f007fc91392d2dff to your computer and use it in GitHub Desktop.
From http://stackoverflow.com/questions/12051470/django-setting-current-user-on-a-model-to-use-in-inlinemodeladmin
Custom save form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from models import BaseModel | |
class BaseAdmin(admin.ModelAdmin): | |
def save_model(self, request, obj, form, change): | |
if not change: | |
obj.created_by = request.user | |
obj.last_updated_by = request.user | |
obj.save() | |
def save_formset(self, request, form, formset, change): | |
instances = formset.save(commit=False) | |
for instance in instances: | |
if isinstance(instance, BaseModel): #Check if it is the correct type of inline | |
if not instance.created_by_id: | |
instance.created_by = request.user | |
instance.last_updated_by = request.user | |
instance.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment