我有一个名为Notification的抽象类。 我有两个具体的实现,即NearNotification和FarNotification。
每个都有一个与它们相关的不同实体。 一个是部门,一个是集团。
我想发送一封电子邮件给与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.emailI 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.emailDepending 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最满意答案
您可以使用self访问该类的名称:
def send(self): if self.__class__.__name__ = "FarNotification": group_email = self.group.email elif if self.__class__.__name__ = "NearNotification": department_email = self.department.emailYou 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更多推荐
发布评论