Ticket #11053: django-onetoonefield.patch

File django-onetoonefield.patch, 1.5 KB (added by Harm Geerts <hgeerts@…>, 16 years ago)
  • django/db/models/fields/related.py

     
    188188            return getattr(instance, self.cache_name)
    189189        except AttributeError:
    190190            params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
    191             rel_obj = self.related.model._base_manager.get(**params)
     191            try:
     192                rel_obj = self.related.model._base_manager.get(**params)
     193            except exceptions.ObjectDoesNotExist:
     194                if self.related.field.null == False:
     195                    raise
     196                rel_obj = None
    192197            setattr(instance, self.cache_name, rel_obj)
    193198            return rel_obj
    194199
  • tests/regressiontests/one_to_one_regress/models.py

     
    141141>>> Target.objects.exclude(pointer2=None)
    142142[]
    143143
     144# return None if the related object does not exist and null=True
     145>>> p = Place()
     146>>> p.bar
     147Traceback (most recent call last):
     148    ...
     149DoesNotExist: Bar matching query does not exist.
     150>>> p.undergroundbar is None
     151True
     152>>> bar = Bar()
     153>>> bar.place is None
     154Traceback (most recent call last):
     155    ...
     156DoesNotExist
     157>>> ubar = UndergroundBar()
     158>>> ubar.place is None
     159True
     160
    144161"""}
Back to Top