OneToOneField('self') problem

37 views
Skip to first unread message

Aldo

unread,
Jul 3, 2008, 8:55:15 AM7/3/08
to Django users
Hello,

I have successfully used OneToOne relationships for inheritance, this
is pretty well documented on the official doc and the result on admin
site is very elegant.
But I cannot use OneToOne field on another simple case : the marriage
case.
The goal is to link 2 objects of the same type together, for example 2
persons which are married.
I tried this very simple model :

class Person(models.Model):
name = models.CharField(max_length=100)
married_to = models.OneToOneField('self', blank = True, null =
True)
def __unicode__(self):
return self.name
class Admin:
pass

I have several problems on the admin site with this model :
-I cannot change 'married_to' field after object creation.
-The OneToOne relationships is not symmetrical as it should be. When I
create Paul married_to Laura, Laura is not automatically married_to
Paul.

I am using today 0.97 trunk SVN version.

Can anybody help me with this model or propose another one ?
Thanks !

Ciao.

Aldo

Malcolm Tredinnick

unread,
Jul 3, 2008, 9:30:56 AM7/3/08
to django...@googlegroups.com

On Thu, 2008-07-03 at 05:55 -0700, Aldo wrote:
[...]

> I have several problems on the admin site with this model :
> -I cannot change 'married_to' field after object creation.

Pretty much no work has been done on OneToOneField handling in existing
(trunk) admin, since it's not worth it with newforms-admin around the
corner. So I'm personally not going to worry too much about things like
this with existing admin.

> -The OneToOne relationships is not symmetrical as it should be.

Well, "as it should be" is a bit of a loaded term. It's not an inherit
property of one-to-one relations that they are symmetrical. Django
happens to have "symmetry by default" for many-to-many, but it's not
clear to me that that should carry over to one-to-one.

> When I
> create Paul married_to Laura, Laura is not automatically married_to
> Paul.
>
> I am using today 0.97 trunk SVN version.
>
> Can anybody help me with this model or propose another one ?

Try ForeignKey(unique=True) and see if that works for you. The only
difference is that the reverse relation returns a list (of one item),
rather than an instance. There's a lot more experience with
ForeignKey('self') than OneToOne('self') -- I've never really considered
doing the latter, so it's not too surprising there might be some bugs
there.

Regards,
Malcolm


Aldo

unread,
Jul 8, 2008, 12:49:22 PM7/8/08
to Django users
Hello,

Thanks for your answer Malcom.
My mistake was to suppose that one to one relationships were
symmetric.

Do you have an idea on how to design a symmetric one to one
relationship ?
I tried with ForeignKey(unique=True) but it is not symmetric.
I even tried ManyToManyField(unique=True), which is symmetric but not
really unique in an object point of view.
It is unique in a DB point of view.
We can add (A,B) and (C,A) tuples in the m2m table, which creates
multiple relationships A-B and A-C.

Is it possible with existing fields ?

Regards,
Aldo
Reply all
Reply to author
Forward
0 new messages