Skip to main content
 首页 » 编程设计

python之NoneType 似乎被评估为 True

2024年12月31日11think

<分区>

我有这个错误:

  File "/boo/foot/routing/models.py", line 133, in indexing 
  contact = self.contact.get_full_name() if self.contact else '', 
AttributeError: 'NoneType' object has no attribute 'get_full_name' 

我的代码是:

class Visit(models.Model): 
    contact = models.ForeignKey(Contact, on_delete=models.SET_NULL, null=True, blank=True ) 
 
 
    def indexing(self): 
        obj = VisitIndex( 
            meta = { 'id' : self.id }, 
            contact = self.contact.get_full_name() if self.contact  else '', 
        ) 
        print(obj) 
        obj.save() 

索引方法附在post_save信号上:

@receiver(post_save, sender = Visit) 
def index_post(sender, instance, **kwargs): 
    instance.indexing() 

我正在测试 self.contact 是否不是假的以便获得 get_full_name()... 然而,似乎即使使用 NoneType 它也能到达那部分。问题出在哪里?