from django.http import HttpResponseRedirect from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME from django.utils.safestring import mark_safe from .models import Orders from django.contrib import admin, messages from django.urls import reverse class OrdersAdmin(admin.ModelAdmin): list_display = ('orderName', 'orderCreateDateTime', 'orderDesc', 'orderAddress', 'show_photo', 'show_photopay') actions =['send_message'] exclude = ['orderAccess', 'userID', 'orderPhoto', 'orderPhotoPay'] def show_photo(self, obj): html = obj.get_photo_html() return mark_safe(html) show_photo.short_description = 'Фото' def show_photopay(self, obj): html = obj.get_photopay_html() return mark_safe(html) show_photopay.short_description = 'Чек' def send_message(orders, request, queryset): selected_objects = request.POST.getlist(ACTION_CHECKBOX_NAME) if len(selected_objects) != 1: messages.error(request, "Выберите только один объект") return selected_user_id = int(selected_objects[0]) obj = queryset.get(orderID=selected_user_id) user_id = obj.userID url = reverse('send_telegram_message', kwargs={'chat_id': user_id}) return HttpResponseRedirect(url) send_message.short_description = 'Отправка сообщения' admin.site.register(Orders, OrdersAdmin)