1
随着简化模型的:检查content_object是某型号
class Notification(models.Model):
content_type = models.ForeignKey(ContentType, null=True)
object_id = models.PositiveIntegerField(null= True)
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Person(models.Model):
name = models.CharField(max_length=50)
我应该如何检查Notification
的content_object是否是Person
类的?
if notification.content_type:
if notification.content_type.name == 'person':
print "content_type is of Person class"
这有效,但它只是不觉得正确,pythonic。有没有更好的办法?
是'notification.content_type.model'是比较合适的。 – Rohan