53👍
✅
You’re very close. You just need to create new LogEntry
objects and save them. LogEntry
has a shortcut function on objects
to do this.
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE
LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(model_object).pk,
object_id=object.id,
object_repr=unicode(object.title),
action_flag=ADDITION if create else CHANGE)
1👍
Actually, Django provides a simpler way these days – you can use built-in methods: log_addition()
, log_change()
and log_deletion()
. See this answer https://stackoverflow.com/a/56705189/14429896
- [Django]-Access instance passed to ModelForm from clean(self) method
- [Django]-Django Queryset and filter() vs get()
- [Django]-Limit foreign key choices in select in an inline form in admin
Source:stackexchange.com