Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 May 13, 2010 17:26:47

T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

OneToOneField usage


Hello Django users,

Is there a particular reason why using a related OneToOneField raises
DoesNotExist instead of returning None?

| class Person( Model ):
| pass
|
| class Pet( Model ):
| owner = OneToOneField( Person )
|
| # Assuming "joe" exists as a Person
| >>> kitty = joe.pet
| DoesNotExist: Pet matching query does not exist.

SingleRelatedObjectDescriptor is expecting the related model to exist.
OneToOneField being a subclass of ForeignKey, my first attempt was:

| >>> kitty = joe.pet.get_or_create()
| DoesNotExist: Pet matching query does not exist.

It's the getter of "joe.pet" which fails. Is it that OneToOneField is
not popular enough to receive such an implementation, or is there a
deeper philosophical reason I don't see?

-Eric

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 May 13, 2010 17:37:55

Peter H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

OneToOneField usage


On May 13, 2010, at 10:29 AM, TallFurryMan wrote:

> Hello Django users,
>
> Is there a particular reason why using a related OneToOneField raises
> DoesNotExist instead of returning None?

Any query you make that is supposed to return one or more instances, that
instead cannot find any results, returns a DoesNotExist.
>
> | class Person( Model ):
> | pass
> |
> | class Pet( Model ):
> | owner = OneToOneField( Person )
> |
> | # Assuming "joe" exists as a Person
> | >>> kitty = joe.pet
> | DoesNotExist: Pet matching query does not exist.

Your process is wrong. You are assuming "joe" exists. But your syntax, "kitty
= joe.pet", assumes that "pet" already exists. You are assigning to "kitty" a
reference to the object named by "joe.pet". If "joe.pet" does not exist, well,
you get a DoesNotExist.
>
> SingleRelatedObjectDescriptor is expecting the related model to exist.
> OneToOneField being a subclass of ForeignKey, my first attempt was:
>
> | >>> kitty = joe.pet.get_or_create()
> | DoesNotExist: Pet matching query does not exist.

This should be:

kitty = Pet.objects.get_or_create(user=joe)

>
> It's the getter of "joe.pet" which fails. Is it that OneToOneField is
> not popular enough to receive such an implementation, or is there a
> deeper philosophical reason I don't see?

No reason, just that your syntax is incorrect.

---Peter Herndon

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 May 13, 2010 19:31:52

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

OneToOneField usage


On May 13, 5:35 pm, Peter Herndon <tphern...@gmail.com> wrote:
> On May 13, 2010, at 10:29 AM, TallFurryMan wrote:
>
> > Hello Django users,
>
> > Is there a particular reason why using a related OneToOneField raises
> > DoesNotExist instead of returning None?
>
> Any query you make that is supposed to return one or more instances, that
> instead cannot find any results, returns a DoesNotExist.

Not true - a .filter() query returns an empty QuerySet if no results
are found. However a .get() query does raise DoesNotExist if it
doesn't find anything - which is the exact equivalent of a
OneToOneField lookup.
>
> > | class Person( Model ):
> > |    pass
> > |
> > | class Pet( Model ):
> > |    owner = OneToOneField( Person )
> > |
> > | # Assuming "joe" exists as a Person
> > | >>> kitty = joe.pet
> > | DoesNotExist: Pet matching query does not exist.
>
> Your process is wrong.  You are assuming "joe" exists.  But your syntax,
> "kitty = joe.pet", assumes that "pet" already exists.  You are assigning to
> "kitty" a reference to the object named by "joe.pet".  If "joe.pet" does not
> exist, well, you get a DoesNotExist.
>
>
>
> > SingleRelatedObjectDescriptor is expecting the related model to exist.
> > OneToOneField being a subclass of ForeignKey, my first attempt was:
>
> > | >>> kitty = joe.pet.get_or_create()
> > | DoesNotExist: Pet matching query does not exist.
>
> This should be:
>
> kitty = Pet.objects.get_or_create(user=joe)

Actually, get_or_create returns two values - the found/created
instance, and a 'created' boolean. So it should be:
kitty, created = Pet.objects.get_or_create(user=joe)

--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 May 13, 2010 20:59:23

T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

OneToOneField usage


Thanks for this insight. I was using OneToOneField in the same way as
ForeignKey.

| class Person( Model ):
| pass
|
| class Pet( Model ):
| owner = ForeignKey( Person )
|
| # Assuming "joe" exists as a Person
| >>> kitty = joe.pet_set.get_or_create()

Yes, in that situation "joe.pet_set" is a QuerySet, not a Model. As
OneToOneField subclasses ForeignKey, this is not immediately clear.

"OneToOneField: a one-to-one relationship. Conceptually, this is
similar to a ForeignKey with unique=True, but the "reverse" side of
the relation will directly return a single object."

I found the ForeignKey syntax very handy, so was trying to apply it
blindly to OneToOneField.

-Eric

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 May 14, 2010 10:43:52

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

OneToOneField usage


On Thu, May 13, 2010 at 7:31 PM, Daniel Roseman <dan...@roseman.org.uk> wrote:
>
>
> On May 13, 5:35 pm, Peter Herndon <tphern...@gmail.com> wrote:
>> On May 13, 2010, at 10:29 AM, TallFurryMan wrote:
>>
>> > Hello Django users,
>>
>> > Is there a particular reason why using a related OneToOneField raises
>> > DoesNotExist instead of returning None?
>>
>> Any query you make that is supposed to return one or more instances, that
>> instead cannot find any results, returns a DoesNotExist.
>
> Not true - a .filter() query returns an empty QuerySet if no results
> are found. However a .get() query does raise DoesNotExist if it
> doesn't find anything - which is the exact equivalent of a
> OneToOneField lookup.

To get super pedantic, he said 'any query that is supposed to return
one or more instances' would raise a DoesNotExist. You countered with
a .filter() query, which returns 0 or more instances.

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 May 14, 2010 15:30:41

Peter H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

OneToOneField usage


On May 14, 2010, at 5:43 AM, Tom Evans wrote:

> On Thu, May 13, 2010 at 7:31 PM, Daniel Roseman <dan...@roseman.org.uk> wrote:
>>
>>
>> On May 13, 5:35 pm, Peter Herndon <tphern...@gmail.com> wrote:
>>> On May 13, 2010, at 10:29 AM, TallFurryMan wrote:
>>>
>>>> Hello Django users,
>>>
>>>> Is there a particular reason why using a related OneToOneField raises
>>>> DoesNotExist instead of returning None?
>>>
>>> Any query you make that is supposed to return one or more instances, that
>>> instead cannot find any results, returns a DoesNotExist.
>>
>> Not true - a .filter() query returns an empty QuerySet if no results
>> are found. However a .get() query does raise DoesNotExist if it
>> doesn't find anything - which is the exact equivalent of a
>> OneToOneField lookup.
>
> To get super pedantic, he said 'any query that is supposed to return
> one or more instances' would raise a DoesNotExist. You countered with
> a .filter() query, which returns 0 or more instances.
>
> Cheers
>
> Tom

:) Indeed, I knew what I *meant* to say, but I must admit I didn't bother to
look up the details before I wrote it all down. Further, I often forget that
get_or_create returns the obj, boolean tuple, leading to the exact bug Daniel
pointed out. I do that in my own code whenever I first use get_or_create again
after an absence, and have to debug it. Mea culpa, I replied carelessly,
points to Daniel for correcting me.

But in *spirit* I was correct! :D Sadly, though, this is neither horseshoes
nor hand-grenades, so close doesn't really count for much.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 June 17, 2010 05:05:28

b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

OneToOneField usage


> Is there a particular reason why using a related OneToOneField raises
> DoesNotExist instead of returning None?
>
> | class Person( Model ):
> |    pass
> |
> | class Pet( Model ):
> |    owner = OneToOneField( Person )
> |
> | # Assuming "joe" exists as a Person
> | >>> kitty = joe.pet
> | DoesNotExist: Pet matching query does not exist.

I have the same problem as TallFurryMan. In this situation, kitty
should be None, rather than an exception being thrown, since the
related object not existing is a valid, non-exceptional condition, and
this would bring symmetry between OneToOneField and ForeignKey. It
also would enable more conveniently checking if such an object does or
doesn't exist, ie:

if joe.pet == None and some_other_condition(): do_stuff()

Rather than having to code such an expression using a try/rescue.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support