Tutorial :django-admin action in 1.1



Question:

I am writing a action in django.I want to now about the rows which are updated by the action or say id field of the row.I want to make a log of all the actions.

I am having a field status which has 3 values :'activate','pending','reject'.I have made action for changing the status to activate.when i perform the action i want to have the log of rows updated so i need some value which can be stored in log such as id coresponding to that row


Solution:1

As far as i can understand you want make an admin log-entry for the object you update using your custom action. I actually did something like that, purely as django does it. As its your custom action you can add this piece of code.

Edit: Call this function after your action finishes, or rather i should say, after you change the status and save 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 fetching LogEntry object

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

Hope this helps.


Solution:2

It is very easy!

Just make a loop of your queryset, then you can access each field of that row and store it where you want.

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »