Check if a OneToOne relation exists in Django
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 … Read more