0

I have an abstract class called Notification. I have 2 concrete implementations which are NearNotification and FarNotification.

Each has a different entity associated with them. One is a Department and one is a Group.

I want to send an email to an email associated with either a Department for a NearNotification or a Group for FarNotifications.

Right now my abstract Notification class looks like:

class Notification(models.Model):
    name = models.CharField(max_length=256)

    class Meta:
        abstract = True

    def send(self):
        group_email = self.group.email
        department_email = self.department.email      

Depending on which class is created, Department or Group the department or Group field is populated.

How can I conditionally sort on this subclass to determine which email to use?

Something like

def send(self):
     if concrete class = FarNotification:
          group_email = self.group.email
     elif if concrete class = NearNotification: 
          department_email = self.department.email
Atma
  • 29,141
  • 56
  • 198
  • 299
  • [This](http://stackoverflow.com/questions/5225556/determining-django-model-instance-types-after-a-query-on-a-base-class) question will help you – Aamir Rind Oct 13 '13 at 22:58

1 Answers1

0

You can access to the name of the class with self:

def send(self):
     if self.__class__.__name__ = "FarNotification":
          group_email = self.group.email
     elif if self.__class__.__name__ = "NearNotification": 
          department_email = self.department.email
Victor Castillo Torres
  • 10,581
  • 7
  • 40
  • 50