2013-10-13 78 views
0

我有一个称为Notification的抽象类。我有两个具体的实现是NearNotification和FarNotification。在django中从抽象基类中获取子类型

每个都有一个与它们相关的不同实体。一个是一个部门,一个是一个集团。

我想发送一封电子邮件到与NearNotification部门或FarNotifications组相关的电子邮件。

现在我的抽象Notification类的样子:

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  

取决于所创建的类,部门或组的部门或组字段填充。

如何有条件地对此子类进行排序以确定要使用哪个电子邮件?

喜欢的东西

def send(self): 
    if concrete class = FarNotification: 
      group_email = self.group.email 
    elif if concrete class = NearNotification: 
      department_email = self.department.email 
+0

[这](http://stackoverflow.com/questions/5225556/determining- django-model-instance-types-after-a-query-on-a-base-class)问题会帮助你 –

回答

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