I have been struggling to resolve this issue for 2 days now without success… Here is my working admin.py that has custom logentries that are coming straight from the model (imnCode)
class imnCodeAdmin(admin.ModelAdmin):
list_display = ('imn_code', 'description', 'killlife', 'addcheck')
@admin.display(description='IMN Code')
def imn_code(self, obj):
return obj.code
@admin.display(description='Description')
def description(self, obj):
return obj.text_value
@admin.display(description='Kill Life')
def killlife(self, obj):
return obj.kill_life
@admin.display(description='Exclude')
def addcheck(self, obj):
return obj.add_check
fieldsets = (
('Change Information', {
'fields': ('code', 'text_value', 'kill_life', 'add_check'),
}),
# Add other fieldsets here if needed
)
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'code':
db_field.verbose_name = 'IMN Code'
elif db_field.name == 'text_value':
db_field.verbose_name = 'Description'
elif db_field.name == 'kill_life':
db_field.verbose_name = 'Kill Life'
elif db_field.name == 'add_check':
db_field.verbose_name = 'Exclude'
return super().formfield_for_dbfield(db_field, **kwargs)
field_custom_names = {
'code': 'IMN Code',
'text_value': 'Description',
'kill_life': 'Kill Life',
'add_check': 'Exclude',
}
def save_model(self, request, obj, form, change):
if change:
obj_before = self.model.objects.get(pk=obj.pk)
changes = []
for field in obj._meta.fields:
field_name = field.attname
old_value = getattr(obj_before, field_name)
new_value = getattr(obj, field_name)
if old_value != new_value:
field_verbose_name = self.field_custom_names.get(field_name, field.verbose_name.capitalize())
change_url = reverse('admin:OCR_imncode_change', args=[obj.pk])
changes.append(f"Changed {field_verbose_name}: <strong>FROM:</strong>{old_value} <strong>TO:</strong> {new_value} <strong>FOR: <a href='{change_url}'>{obj.code}</a></strong>")
if changes:
content_type = ContentType.objects.get_for_model(obj)
LogEntry.objects.create(
user_id=request.user.pk,
content_type_id=content_type.pk,
object_id=obj.pk,
object_repr=str(obj),
action_flag=CHANGE,
change_message=", ".join(changes),
)
super().save_model(request, obj, form, change)
def log_change(self, request, obj, message):
pass
def log_deletion(self, request, obj, object_repr):
deleted_info = f'<strong>Deleted </strong>"{obj.code}", <strong>Description: </strong>{obj.text_value}'
LogEntry.objects.create(
user_id=request.user.pk,
content_type_id=ContentType.objects.get_for_model(obj).pk,
object_id=obj.pk,
object_repr=object_repr,
action_flag=DELETION,
change_message=deleted_info,
)
def log_addition(self, request, obj, message):
change_url = reverse('admin:OCR_imncode_change', args=[obj.pk])
added_info = f'<strong>Added <a href="{change_url}">{obj.code}</a> , Description: </strong>{obj.text_value}, <strong>Kill Life</strong> {obj.kill_life}'
LogEntry.objects.create(
user_id=request.user.pk,
content_type_id=ContentType.objects.get_for_model(obj).pk,
object_id=obj.pk,
object_repr=str(obj),
action_flag=ADDITION,
change_message=added_info,
)
admin.site.register(imnCode, imnCodeAdmin)
but now the idea is to add new model called “Department” and to access imncodes only within the Department inline.
So each department will have their own imn codes.
But when I try to create a custom logentries this way then I can’t manage to work it out and I always get default LogEntries. Tried to apply same principle but it looks like just because it is inline and not direct change,deletion or addition then it doesn’t work.
class imnCodeInline(admin.TabularInline):
model = imnCode
class DepartmentAdmin(admin.ModelAdmin):
inlines = [imnCodeInline]
#here goes custom logs!
admin.site.register(Department, DepartmentAdmin)
Maybe somebody has figured this out before and can share the solution? Thanks in advance!