Use the value of a field inside an administration action


I would like to be able to use the value of a common object field inside an administration action. I tried to do this with the too many parameters (self, request, queryset) but I can’t do it.
Need help.

I think you’ll need to be more specific as to what exactly you’re trying to accomplish. (What do you mean by “a common object field” in this context? How are you trying to use this value? Where are you trying to use it?) You also may attract quicker help if you include the relevant code of how you’ve tried to approach this.

class MyModelAdmin(admin.ModelAdmin):

readonly_fields = ('code', 'nom', 'prenom', 'sexe', 'datenaiss', 'lieunaiss', 'annee_bepc', 'centre_bepc', 'numtable',
               'datecreation', 'datecreation', 'datemodification', 'image_tag',)
#list_editable = ('env1', 'env2')
list_display = ('image_tag', 'code', 'nom', 'prenom', 'sexe', 'datenaiss', 'lieunaiss', 'annee_bepc', 'centre_bepc',
               'datecreation', 'env1', 'env2', 'valide_poste', 'param_secret' )

search_fields = ('nom', 'prenom', 'code', 'numtable', 'datenaiss', 'lieunaiss', 'numpi', 'numtel', 'env1', 'env2',)
list_filter = (
    ('annee_bepc', DropdownFilter),
    ('sexe', DropdownFilter),
    ('valide_agence_id__agence', DropdownFilter),
#filter_horizontal = ['annee_bepc', 'sexe']
ordering = ('-code', 'datecreation', 'nom', 'prenom', 'datenaiss', 'lieunaiss', 'nompere', 'nommere', 'sexe',)
actions = ['auth_valide_poste', 'auth_imp_recu_poste', ]
actions_on_bottom = True

class Media:
    js = [

def get_readonly_fields(self, request, obj=None):

    # Group Poste
    if request.user.groups.filter(name='poste').exists():
        readonly_fields = ('code', 'nom', 'prenom', 'sexe', 'datenaiss', 'lieunaiss', 'annee_bepc', 'centre_bepc',
                           'numtable', 'numtel', 'email', 'numpi',
               'datecreation', 'datecreation', 'datemodification', 'image_tag', 'photo_cand', 'nompere', 'nommere', 'valide_poste')
        return  readonly_fields

    # Group Superuser
    if request.user.is_superuser is True:
        readonly_fields = ('code', 'datecreation', 'image_tag', )
        return  readonly_fields

    return super().get_fieldsets(request, obj=obj)

def auth_imp_recu_poste(self, request, queryset):
    if not request.user.has_perm('concours.auth_valide_poste'):
        messages.error(request, 'Vous n\'êtes autorisé(e) à effectuer cette action')
        #k = 'be186847-9aaf-45db-a1bb-ae966979bf52'
        r = MyModel.objects.get(code__exact=code).param_secret
        k = r
        return HttpResponseRedirect(reverse('generatePdfAuth', args=[k]))

auth_imp_recu_poste.short_description = "Imprimer les reçu de paiement Poste-CI"

Here is my code ( I would like inside the ‘auth_imp_recu_poste’ administration action to have access to the ‘param_secret’ field to send it as a parameter to one of my views.

The queryset parameter you’re receiving is the queryset defining the individual objects to be acted upon. If you want to do something with each of the selected objects, you need to iterate over that queryset.

Your query on MyModel references a variable named “code” that doesn’t seem to be defined within this method.
r = MyModel.objects.get(code__exact=code).param_secret
I’m guessing you want to retrieve this value for each member of the queryset.

What does your entry for generatePdfAuth look like? (Does it have a parameter for a code to be passed to it?) This also doesn’t look like it’s going to work if multiple rows are selected - the first return is going to prevent any of the other selected objects from being processed.

You might want to take a step back and reread the Admin actions docs, particularly the sections on Actions that provide intermediate pages.

If you’re going to allow multiple selections to be processed, you might also want to change how you’re forwarding data from this action to your view that is going to process those selections.

‘code’ is a field of my MyModel model. I don’t know what to call it in this admin action. ‘param_secret’ is another field in my template and it is a parameter of the ‘generatePdfAuth’ view. This view is used to generate a pdf document. It works fine when it receives a ‘param_secret’ parameter. I come back to the problem: How to correctly call ‘code’ or ‘param_secret’ inside the admin action ???

As posted above:

Your function can end up being called with a reference to multiple objects, not just one.

How??? please one example.

It’s all documented in the links above - probably better than I would be able to describe it.