我无法看到如何使其工作...
我的模型中有一个名为has_related_object
的方法,需要检查是否存在相关对象...
class Business(base):
name = models.CharField(max_length=100, blank=True, null=True)
def has_related_object(self):
has_customer = False
has_car = False
try:
has_customer = (self.customer is not None)
except Business.DoesNotExist:
pass
try:
has_car = (self.car.park is not None)
except Business.DoesNotExist:
pass
return has_customer and has_car
class Customer(base):
name = models.CharField(max_length=100, blank=True, null=True)
person = models.OneToOneField('Business', related_name="customer")
错误
RelatedObjectDoesNotExist Business has no customer.
我需要检查这些相关对象是否存在,但是要从业务对象方法内部进行。