Django check if a related object exists error: RelatedObjectDoesNotExist

前端 未结 2 1853
陌清茗
陌清茗 2021-01-30 12:10

I have a method has_related_object in my model that needs to check if a related object exists

class Business(base):
      name =  models.CharField(ma         


        
2条回答
  •  伪装坚强ぢ
    2021-01-30 13:06

    Use hasattr(self, 'customers') to avoid the exception check as recommended in Django docs:

    def has_related_object(self):
        return hasattr(self, 'customers') and self.car is not None
    

提交回复
热议问题