Opened 12 years ago

Closed 10 years ago

#3016 closed defect (duplicate)

make reverse one-to-one field return None instead of DoesNotExist

Reported by: shaunc <shaun@…> Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: normal Keywords: 121-rewrite
Cc: mir@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


in django.db.models.fields.related.SingleRelatedObjectDescriptor, The
code for get:

    def __get__(self, instance, instance_type=None):
        if instance is None:
            raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
        params = {'%s__pk' % instance._get_pk_val()}
        rel_obj = self.related.model._default_manager.get(**params)
        return rel_obj

Here, if "instance._get_pk_val()" returns None, then the get(params)
will return *any* object.

(The primary key could be None for a new, unsaved object, for

More sensible would either to be to return None or raise

Change History (9)

comment:1 Changed 12 years ago by mir@…

Cc: mir@… added

I think the behaviours of filter(xxx=None) and get(xxx=None) have changed recently and should return an empty QuerySet or raise model.DoesNotExist, respectively. Did you use a recent version from svn?

comment:2 Changed 12 years ago by shaunc <shaun@…>

You're right! I was running on r3900. The latest one's ...get() returns DoesNotExist. And that behavior is documented, too! So... "oh well -- fixed again!" :)

Thanks for looking in to it; I have to remember how fast the project is moving.

comment:3 Changed 12 years ago by Chris Beaven

Resolution: fixed
Status: newclosed

Fixed by downloading latest SVN according to ticket submitter.

comment:4 Changed 12 years ago by shaunc <shaun@…>

Resolution: fixed
Status: closedreopened

Actually, now that I think about it, I'm not sure that raising DoesNotExist is the best behaviour.

For instance, if model Foo has a ForeignKey bar, and a reverse one-to-one field baz,

    foo = Foo()


    if ...

will skip the if body if bar doesn't exist, but

    if foo.baz: ...

will raise "DoesNotExist".

It seems to me that they should behave the same: dereference should return none for missing object, or both should throw DoesNotExist.

comment:5 Changed 11 years ago by James Bennett

#3484 is a subset of this, and so was marked as a duplicate.

comment:6 Changed 11 years ago by Philippe Raoult

Triage Stage: UnreviewedDesign decision needed

comment:7 Changed 11 years ago by Philippe Raoult

Summary: related object descriptor returns arbitrary object when called from unsaved starting objectmake reverse one-to-one field return None instead of DoesNotExist

comment:8 Changed 11 years ago by Jacob

Keywords: 121-rewrite added
Triage Stage: Design decision neededAccepted

We'll look at this again when we refactor one-to-one, but my gut is that returning None is the right behavior.

comment:9 Changed 10 years ago by Malcolm Tredinnick

Resolution: duplicate
Status: reopenedclosed

Closing as a dupe of #10227. I realise this is the older ticket, but the description here is less useful than the one in the other ticket (which describes the user-facing behaviour).

Note: See TracTickets for help on using tickets.
Back to Top