has_related_object
我的模型中有一个方法需要检查相关对象是否存在
class Business(base):
name = models.CharField(max_length=100, blank=True, null=True)
def has_related_object(self):
return (self.customers is not None) and (self.car is not None)
class Customer(base):
name = models.CharField(max_length=100, blank=True, null=True)
person = models.OneToOneField('Business', related_name="customer")
但是我得到了错误:
Business.has_related_object()
RelatedObjectDoesNotExist:公司没有客户。