The python Django study notes (five) --- background (admin.py) Action Use

Action that is a plug-in interface field list above

Only delete a default action: Delete selected table name. This can disable this action, in line admin.py increase in the following code:

class StudentAdmin(admin.ModelAdmin):
    list_display = ('NameNo','Sex_color','Age')

admin.site.register(Student,StudentAdmin)
admin.site.disable_action('delete_selected')#禁用默认Action

 Ban delete function, you can also customize a delete function:

def delete_selected(modeladmin,request,queryset):#自定义删除动作
    a = queryset.delete()[0]
    if a == 1:
        message = '1 item is deleted!'
    else:
        message = '%d items are deleted!'%a
    modeladmin.message_user(request, message)
delete_selected.short_description = '删除选中项'

class StudentAdmin(admin.ModelAdmin):
    list_display = ('NameNo','Sex_color ' , ' Age ' ) 
    Actions = (delete_selected,) # add a custom action to the Action

Refresh the interface you can see Aciton action which has been newly added:

Here is a rewrite of the delete_selected this method, even if the above does not disable this method, there will only display method to add. (If not join in the action, or will display the original method)

 

Of course, this action can Delete_selected use all the models, and disable similar, just one line of code:

admin.site.add_action (delete_selected) # All models have this action

 

actions:

In this case, other actions may also be custom, then added to the Action, such as modifying the selected records gender as male or female

Add the following code in admin.py:

def change_sex_female(modeladmin,request,queryset):
    a = queryset.update(Sex = 'F')
    if a == 1:
        message = '1 item is updated to Female!'
    else:
        message = '%d items are updated to Female!'%a
    modeladmin.message_user(request,message)#修改成功后的提示
change_sex_female.short_description = '修改性别为Female'

def change_sex_male(modeladmin,request,queryset):
    a = queryset.update(Sex = 'M')
    if a == 1:
        message = '1 item is updated to Male!'
    else:
        message = '%d items are updated to Male!'%a
    modeladmin.message_user(request,message)#修改成功后的提示
change_sex_male.short_description = '修改性别为Male'

class StudentAdmin(admin.ModelAdmin):
    list_display = ('NameNo','Sex_color', ' Age ' ) 
    Actions = (delete_selected, change_sex_female, change_sex_male,) # the two self-defined actions Add Action

The code adds two actions change_sex_female and change_sex_male, then StudentAdmin in these two actions joined actions, refresh the screen you can see has added two more Action:

Can select several data and then execute, there will be tips:

 

Operation can also be reversed according to the current Gender:

def change_sex(modeladmin,request,queryset):
    if queryset.filter(Sex='F').count() == 1 or queryset.filter(Sex='M').count() == 1:
        if queryset.filter(Sex='F'):
            queryset.update(Sex='M')
            message = 'Item is changed to Male!'
        else:
            queryset.update(Sex='F')
            message = 'Item is changed to Female!'
    else:
        message = 'Can\'t select more than 1 item!'

    modeladmin.message_user(request,message)
change_sex.short_description = '性别反转'

class StudentAdmin(admin.ModelAdmin):
    list_display = ('NameNo','Sex_color','Age')
    actions = (change_sex_female,change_sex_male,delete_selected,change_sex,)

 

If the actions set to None , this plug-in does not appear Action

Similarly, if the setting is disabled and no action Delete_selected set other Action, name will not appear Aciton this plugin

 

action_on_top:

Default True displayed at the top, if set to False will not be displayed at the top

 

action_on_bottom:

False default not displayed, if set to True is displayed in the lower end

 

actions_selection_counter:

Default True, statistics show the right of the plug-in, if set to False, then the statistics will not be displayed

 

 

 

 

 

 

 

 

 

 

 data

 https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

https://docs.djangoproject.com/en/dev/ref/contrib/admin/

 

Guess you like

Origin www.cnblogs.com/watertaro/p/10634552.html