Django-admin action in 1.1

I am writing an action in django.I want now about the lines that are updated in the action or say the id field of the line. I want to make a log of all actions.

I have a field status that has 3 meanings: “activate”, “wait”, “reject”. I took an action to change the status for activation. When I perform an action, I want to have the line log updated, so I need some value that can be saved in the log, for example, the id corresponding to this line

+3
source share
2 answers

As I understand it, you want to create an administrator account for the object that you are updating using your action. I actually did something similar, just like django. As your custom action, you can add this piece of code.

Change . Call this function after the action completes, or rather, I have to say, after changing the state and saving the object.

def log_it(request, object, change_message):
    """
    Log this activity
    """
    from django.contrib.admin.models import LogEntry
    from django.contrib.contenttypes.models import ContentType

    LogEntry.objects.log_action(
        user_id         = request.user.id, 
        content_type_id = ContentType.objects.get_for_model(object).pk, 
        object_id       = object.pk, 
        object_repr     = change_message, # Message you want to show in admin action list
        change_message  = change_message, # I used same
        action_flag     = 4
    )

# call it after you save your object
log_it(request, status_obj, "Status %s activated" % status_obj.pk) 

You can always get which object you updated by retrieving the LogEntry object

log_entry = LogEntry.objects.filter(action_flag=4)[:1]
log_entry[0].get_admin_url()

Hope this helps.

+9
source

It is very easy!

Just create your request loop, then you can access each field of this line and save it where you want.

for e in queryset:          
   if (e.status != "pending"):
       flag = False
0
source

Source: https://habr.com/ru/post/1708410/


All Articles