获取通用外键的属性
Get attribute of a generic foreign key
我仍在努力研究 Django 中的通用外键,所以到目前为止我所想出的是非常基础的。我正在尝试创建一个 NotificationRecipient
,它具有我已经创建的 2 个不同模型的通用外键。这些通知收件人可以是 Client
或 Account
。我可能会决定添加更多新模型的接收者。
我想在 NotificationRecipient 中创建一个 get_email
方法,用于检查收件人是联系人、客户还是帐户。然后根据它是哪个模型,它会提取不同的属性。
我现有的模型看起来有点像这样:
class Client(models.Model):
primary_email = models.EmailField(blank=True)
...
class Account(AbstractNamedUser):
email = models.EmailField(blank=True)
...
尝试根据型号获取邮件:
class NotificationRecipient(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def get_email_addr(self):
''' Gets the model, then pulls the email address. '''
# Get the model
# if client:
# return primary_email
# elif account:
# return email
我该怎么做?
您可以检查 content_type
字段以确定对象的种类。
但与其检查类型,不如考虑在所有目标模型上定义一个属性,returns 相关属性。
最好在目标模型上定义一个方法,而不是在 NotificationRecipient 模型中构建所有业务逻辑。
逻辑是 NotificationRecipient 模型只需要知道它需要一个电子邮件地址。
class Client(...):
def get_email_addr(self):
return primary_email
class Account(...):
def get_email_addr(self):
return email
class NotificationRecipient(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def get_email_addr(self):
try:
email = self.content_object.get_email_addr()
# if you want to enforce the attribute
except AttributeError:
raise ImproperlyConfigured('Model requires an email address')
# if you need a default
if not email:
return 'default@email.com'
return email
我仍在努力研究 Django 中的通用外键,所以到目前为止我所想出的是非常基础的。我正在尝试创建一个 NotificationRecipient
,它具有我已经创建的 2 个不同模型的通用外键。这些通知收件人可以是 Client
或 Account
。我可能会决定添加更多新模型的接收者。
我想在 NotificationRecipient 中创建一个 get_email
方法,用于检查收件人是联系人、客户还是帐户。然后根据它是哪个模型,它会提取不同的属性。
我现有的模型看起来有点像这样:
class Client(models.Model):
primary_email = models.EmailField(blank=True)
...
class Account(AbstractNamedUser):
email = models.EmailField(blank=True)
...
尝试根据型号获取邮件:
class NotificationRecipient(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def get_email_addr(self):
''' Gets the model, then pulls the email address. '''
# Get the model
# if client:
# return primary_email
# elif account:
# return email
我该怎么做?
您可以检查 content_type
字段以确定对象的种类。
但与其检查类型,不如考虑在所有目标模型上定义一个属性,returns 相关属性。
最好在目标模型上定义一个方法,而不是在 NotificationRecipient 模型中构建所有业务逻辑。
逻辑是 NotificationRecipient 模型只需要知道它需要一个电子邮件地址。
class Client(...):
def get_email_addr(self):
return primary_email
class Account(...):
def get_email_addr(self):
return email
class NotificationRecipient(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def get_email_addr(self):
try:
email = self.content_object.get_email_addr()
# if you want to enforce the attribute
except AttributeError:
raise ImproperlyConfigured('Model requires an email address')
# if you need a default
if not email:
return 'default@email.com'
return email