Opened 18 years ago

Closed 15 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

Description

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' % self.related.field.name: 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
instance.)

More sensible would either to be to return None or raise
self.related.model.DoesNotExist.

Change History (9)

comment:1 by mir@…, 18 years ago

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 by shaunc <shaun@…>, 18 years ago

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 by Chris Beaven, 18 years ago

Resolution: fixed
Status: newclosed

Fixed by downloading latest SVN according to ticket submitter.

comment:4 by shaunc <shaun@…>, 18 years ago

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()

Now:

    if foo.bar: ...

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 by James Bennett, 17 years ago

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

comment:6 by Philippe Raoult, 17 years ago

Triage Stage: UnreviewedDesign decision needed

comment:7 by Philippe Raoult, 17 years ago

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 by Jacob, 16 years ago

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 by Malcolm Tredinnick, 15 years ago

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