Now I'm using django 1.6
I have two models relates with a OneToOneField
.
class A(models.Model):
pass
class B(models.Model):
ref_a = models.OneToOneField(related_name='ref_b', null=True)
First see my code that points out the problem:
a1 = A.objects.create()
a2 = A.objects.create()
b1 = B.objects.create()
b2 = B.objects.create(ref_a=a2)
# then I call:
print(a1.ref_b) # DoesNotExist Exception raised
print(a2.ref_b) # returns b2
print(b1.ref_a) # returns None
print(b2.ref_a) # returns a2
Now the problem is, if I want to check a A
object, to judge whether it exists a B
objects referencing it. How can I do?
The valid way I tried is only to try and catch an exception, but is there any other prettier way?
My effort:
1 - The below code works, but is too ugly!
b = None
try:
b = a.ref_b
except:
pass
2 - I also tried to check the attributes in a, but not working:
b = a.ref_b if hasattr(a, 'ref_b') else None
Do you meet the same problem, friends? Please point me a way, thank you!
hasattr
seems to work fine for me on Django 1.8.17 with Python 2. – Fouts