-2

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
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • 1
    Please add your code into the question directly and do not use images or links for it, which is not allowed. For more information: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Michael Hawkins Aug 03 '20 at 14:15
  • 1
    Here. Tnx for the heads up. – CodingByDay Aug 03 '20 at 14:26
  • Don't edit the title as "Solved". Just accepting an answer is enough. – Asocia Aug 03 '20 at 17:19

1 Answers1

0

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()
rgermain
  • 708
  • 4
  • 11
  • The above exception (table main_tickets has no column named email_as_send) was the direct cause of the following exception: – CodingByDay Aug 03 '20 at 15:19
  • the email_as_send is a new field , you need to makemigrations and migrate .. – rgermain Aug 03 '20 at 15:24
  • I know, but when i migrated and tried it out in the django admin page, I changed one tickets status and status_as_send to true, and it didn't send an email to the user. – CodingByDay Aug 03 '20 at 15:41
  • is the def save(self) function in the models or somewhere else? – CodingByDay Aug 03 '20 at 15:59
  • in the models, and have you do configure email in django ? – rgermain Aug 03 '20 at 16:07
  • i did configured it. I programmed it so when u post the ticket it sends an email to an admin. But then when i wrote your code to purpose was that If i change status to true in the admin panel it sends an email to a user who posted it, Email to admin works, this doesnt. – CodingByDay Aug 03 '20 at 16:12
  • to send email is need to status set True, and email_as_send to false, check in admin, and change to this send_mail(....., fail_silently=False) – rgermain Aug 03 '20 at 16:23
  • Man this works. Thank you I cant say that enough. Thanks for the patience I just finished this project. With metta _/I\_ – CodingByDay Aug 03 '20 at 16:44