Why my def perform_update() doesn’t work in Django rest framwrok?

i want when user update a data of leave request django will send email to supervisor of that user

perform_create is work but perform_update don’t work

Here’s my code that’s problem

        def perform_update(self, serializer):
            with transaction.atomic():
                try:
                    leave_request = serializer.instance
                    serializer.save()
                    supervisor = leave_request.user.supervisor
                    if supervisor and supervisor.email:
                        self.send_leave_request_email(leave_request, supervisor.email, "Updated Leave Request")
                except Exception as e:
                    logger.error(f"Error during leave request update: {str(e)}")
                    raise e

Here’s CLass

class LeaveRequestList(generics.ListCreateAPIView):
        serializer_class = leave_requestsSerializer

        def get_queryset(self):
                queryset = leave_requests.objects.all()
                location = self.request.query_params.get('location')
                if location is not None:
                    queryset = queryset.filter(testLocation=location)
                return queryset

        def perform_create(self, serializer):
            with transaction.atomic():
                try:
                    leave_request = serializer.save()
                    supervisor = leave_request.user.supervisor
                    if supervisor and supervisor.email:
                        self.send_leave_request_email(leave_request, supervisor.email, "Leave Request")
                except Exception as e:
                    logger.error(f"Error during leave request creation: {str(e)}")
                    raise e
                    
        def perform_update(self, serializer):
            with transaction.atomic():
                try:
                    leave_request = serializer.instance
                    serializer.save()
                    supervisor = leave_request.user.supervisor
                    if supervisor and supervisor.email:
                        self.send_leave_request_email(leave_request, supervisor.email, "Updated Leave Request")
                except Exception as e:
                    logger.error(f"Error during leave request update: {str(e)}")
                    raise e


        def send_leave_request_email(self, leave_request, supervisor_email, subject):
            try:
                datetime_start_formatted = leave_request.datetime_start.strftime("Date: %d %B %Y Time: %I:%M:%S %p")
                datetime_end_formatted = leave_request.datetime_end.strftime("Date: %d %B %Y Time: %I:%M:%S %p")
                duration = (leave_request.datetime_end - leave_request.datetime_start)
                days = duration.days
                hours, remainder = divmod(duration.seconds, 3600)
                minutes, _ = divmod(remainder, 60)
                first_name = leave_request.user.first_name
                last_name = leave_request.user.last_name    

                email_html_message = f"""
                <!DOCTYPE html>
                <html>
                <head>
                    <title>{subject}</title>
                    <style>
                        body {{
                            font-family: Arial, sans-serif;
                            background-color: #f4f4f4;
                            margin: 0;
                            padding: 0;
                        }}
                        .container {{
                            max-width: 800px;
                            margin: 0 auto;
                            padding: 20px;
                            background-color: #fff;
                            border-radius: 10px;
                            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                        }}
                        h1 {{
                            color: #333;
                        }}
                        p {{
                            color: #555;
                        }}
                        .details {{
                            margin-top: 20px;
                            padding-top: 10px;
                            border-top: 1px solid #ccc;
                        }}
                    </style>
                </head>
                <body>
                    <div class="container">
                        <h1>{subject}</h1>
                        <p>Intern <strong>{first_name} {last_name}</strong> has requested leave:</p>
                        <div class="details">
                            <p><strong>Requested Period:</strong> <strong>[ {datetime_start_formatted} ]</strong> to <strong>[ {datetime_end_formatted} ]</strong></p>
                            <p><strong>Duration:</strong> {days} days, {hours} hours, {minutes} minutes</p>
                            <p><strong>Type of Leave:</strong> {leave_request.get_type_of_leave_display()}</p>
                            <p><strong>Reason:</strong> {leave_request.description}</p>
                            <p><strong>Contact:</strong> {leave_request.tel}</p>
                        </div>
                    </div>
                </body>
                </html>
                """
                send_mail(
                    f"{subject} from {first_name} {last_name}",
                    "",
                    "VANMAN System <[email protected]>",
                    [supervisor_email],
                    html_message=email_html_message,
                    fail_silently=False,
                )
            except Exception as e:
                logger.error(f"Error sending leave request email: {str(e)}")
                raise e
    
        def delete(self, request, *args, **kwargs):
            try:
                # Delete all instances of leave_requests
                leave_requests.objects.all().delete()
                return Response({'message': 'All leave_requests deleted successfully'}, status=status.HTTP_204_NO_CONTENT)
            except Exception as e:
                return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

New contributor

user25319994 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật