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
4

1 回答 1

0

您可以通过以下方式访问类的名称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
于 2013-10-14T01:35:08.423 回答