views:

1235

answers:

2

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

+4  A: 

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.

simplyharsh
as far as i know django doesnot mantain any log.Can U explain little more.Where i have to use this function with respect to my action
ha22109
If you talk about django admin, it DOES maintain a log of which object is altered by what kind of action (i.e. ADDED, CHANGED, DELETED). If django doesn't maintain any log, whats the action list in your default admin home?? Its a log of actions. Check "django.contrib.admin.models.LogEntry". In above answer, i just added a fourth alternative of action, which you invoke from your action. I guess you noted, "action_flag = 4". I edited the answer, for you to know where to call it.
simplyharsh
A: 

it is very easy

just make a loop of your queryset,then u can access each field of that row and store it where u want

for e in queryset:

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

ha22109