Skip to content
Snippets Groups Projects
models.py 819 B
Newer Older
  • Learn to ignore specific revisions
  • Rafael László's avatar
    Rafael László committed
    from django.db import models
    from account.models import Profile
    
    from django.dispatch import receiver
    
    Rafael László's avatar
    Rafael László committed
    
    
    class Mentor(models.Model):
        mentor = models.ForeignKey(
            Profile,
            related_name="mentor",
            on_delete=models.DO_NOTHING,
        )
        name = models.CharField(null=False, max_length=200)
        text = models.TextField()
    
    Rafael László's avatar
    Rafael László committed
        image = models.ImageField(
    
            upload_to='public/mentors/images/', null=True, blank=True)
    
    Rafael László's avatar
    Rafael László committed
        email = models.EmailField()
    
        def __str__(self):
            return self.name
    
    
    # Deletes file from filesystem when File object is deleted.
    @receiver(models.signals.post_delete, sender=Mentor)
    def auto_delete_image_on_delete(sender, instance, **kwargs):
        if instance.image:
            if os.path.isfile(instance.image.path):
                os.remove(instance.image.path)