Reputation: 4008
I have the following model form which I inherit in the majority of my models.
class UserStamp(models.Model):
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True,
related_name='%(app_label)s_%(class)s_created_by', on_delete=models.CASCADE)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True,
related_name='%(app_label)s_%(class)s_updated_by', on_delete=models.CASCADE)
class Meta:
abstract = True
Where I construct my own forms I use
form.instance.created_by = self.request.user
and everything is ok.
But not in admin (expecting staff users) because he uses his own forms. What is the simplest way to make the admin forms work properly(something tha I don't need to do each of them from zero).
Upvotes: 0
Views: 45
Reputation: 599600
You can override save_model
in your ModelAdmin class.
class MyModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.created_by = request.user
obj.save()
Upvotes: 1