Django Admin Customizing

前端 未结 2 985
青春惊慌失措
青春惊慌失措 2021-02-06 19:38

I am designing an admin interface where invite mails will be sent to users. My Invitation model is ready & in my invitation admin interface I am able to see my added users f

2条回答
  •  深忆病人
    2021-02-06 20:18

    Regarding the send button for each row, you can give your model (or ModelAdmin) a new function which returns the corresponding HTML pointing to your views (or calling corresponding AJAX functions). Just add your function to the ModelAdmin's "list_display" and make sure that HTML tags don't get escaped:

    class MyModelAdmin(admin.ModelAdmin):
        ...
        list_display = ('name', 'email', 'sender', 'send_email_html')
    
        def send_email_html(self, obj):
            # example using a javascript function send_email()
            return 'Send Now' % obj.id
        send_email_html.short_description = 'Send Email'
        send_email_html.allow_tags = True
    

    Regarding the use of an action, define "actions" in your ModelAdmin as a list containing your function which takes modeladmin, request, queryset as parameters:

    def send_email_action(modeladmin, request, queryset):
        whatever_you_want_to_do_with_request_and_queryset
    send_email.short_description = 'Send email'
    
    class MyModelAdmin(admin.ModelAdmin):
        ...
        actions = [
            send_email_action
        ]
    

提交回复
热议问题