简体   繁体   中英

How to check the status field in models django and send mail to the user?

I wrote a fullstack django ticketing system for a company. Everything works, login, register, posting a ticket, database, send mail to admin. Only thing that is left is to check the status field of the ticket and if its true send an email to the user who posted it.

class tickets(models.Model):
    name = models.ForeignKey(devices, on_delete=models.CASCADE,     blank=True, null=True)
    location = models.CharField(max_length=200)       
    company = models.CharField(max_length=200)
    serial_number = models.CharField(max_length=200)
    problem = models.CharField(max_length=1000)
    contact_number = models.CharField(max_length=200)
    status = models.BooleanField(default=False)
    author = models.ForeignKey(User, on_delete=models.CASCADE,   r elated_name="authorname", null=True)
    executive = models.ForeignKey(executives, on_delete=models.CASCADE, blank=True, null=True)

def __str__(self):
    return self.problem

you can do like this

from django.core.mail import send_mail

class tickets(models.Model):
   email_as_send = models.BooleanField(default=False)
    ...

   def save(self):
       if self.status and self.email_as_send:
          send_mail("subject", "message","your_website@email.fr", [self.author.email])
          self.email_as_send = True
       super().save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM