import os from django.db import models from django.contrib.auth import get_user_model from tickets.models.ticket import Ticket def get_attachment_upload_dir(instance, filename): return "/".join(["attachments", str(instance.ticket.pk), filename]) class Attachment(models.Model): ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE) added_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) timestamp = models.DateTimeField(auto_now_add=True) file = models.FileField(upload_to=get_attachment_upload_dir, max_length=255) def filename(self): return os.path.basename(self.file.name) def extension(self): name, extension = os.path.splitext(self.file.name) return extension def __str__(self): return f"{self.ticket.pk} - {self.file.name}"