Check if a OneToOne relation exists in Django

心已入冬 提交于 2019-12-03 10:39:51
Mr.Coffee

So you have a least two ways of checking that. First is to create try/catch block to get attribute, second is to use hasattr.

class A(models.Model):
   def get_B(self):
       try:
          return self.b
       except:
          return None

class B(models.Model):
   ref_a = models.OneToOneField(related_name='ref_b', null=True)

Please try to avoid bare except: clauses. It can hide some problems.

The second way is:

class A(models.Model):
    def get_B(self):
       if(hasattr(self, 'b')):
           return self.b
       return None

class B(models.Model):
    ref_a = models.OneToOneField(related_name='ref_b', null=True)

In both cases you can use it without any exceptions:

a1 = A.objects.create()
a2 = A.objects.create()
b1 = B.objects.create()
b2 = B.objects.create(ref_a=a2)

# then I call:
print(a1.get_b)  # No exception raised
print(a2.get_b)  # returns b2
print(b1.a)  # returns None
print(b2.a)  # returns a2

There is no other way, as throwing the exception is default behaviour from Django One to One relationships.

And this is the example of handling it from official documentation.

>>> from django.core.exceptions import ObjectDoesNotExist
>>> try:
>>>     p2.restaurant
>>> except ObjectDoesNotExist:
>>>     print("There is no restaurant here.")
There is no restaurant here.

Individual model classes provide a more specific exception called DoesNotExist that extends ObjectDoesNotExist. My preference is to write it this way:

b = None
try:
    b = a.ref_b
except B.DoesNotExist:
    pass

hasattr works fine with Django1.11 ! You may use getattr for shorter version:

getattr(self, 'field', default)

In your case

b = getattr(a, 'ref_b', None)

https://docs.python.org/3.6/library/functions.html#getattr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!