On Thu, 2009-05-07 at 07:12 -0700, Aldo wrote: > Hello, > > I have a problem for a quite simple model. > I would like to model a person marriage relation. > > example : > > class Person(models.Model): > name=models.CharField(max_length=100) > married_to=models.OneToOneField('self',symmetrical=True) # > symmetrical not allowed > married_to=models.ForeignKey > ('self',unique=True,symmetrical=True) # symmetrical not allowed
This is because "symmetrical" requires two entries (or making two filters every time you look something up). Only many-to-many fields can be symmetrical, because they have an entry for the pair (A, B) and another entry for (B, A). > married_to=models.ManyToManyField > ('self',unique=True,symmetrical=True) # unique not allowed It's called many-to-many. Multiple entries, pretty much by definition. Also, as noted, symmetrical and unique can't work together on an implementation level. > > The relation I want has to be unique and symmetrical. > Someone can only be married to one other person (unique). > If Alice is married to Bob, then Bob is automatically married to Alice > (symmetrical). > > How would you model this relation There are many relations between data that simply cannot be modelled at the model level. Even modelling them at the database level isn't always possible. In this case, you get to choose which way you want to model things at the Model subclass level and then enforce the remaining portions in, say, the save() method or in the forms you're using to populate the model. You could choose to have a nullable OneToOneKey between Person objects and remember to check both whether the relation or the reverse relation exists when trying to add a new one. Or your could use a ManyToManyField and check that no entry already exists when adding a new one. In either case, some extra checking will be required by you. That's simple because this constraint is sufficiently complex that it's not covered by default (even writing an SQL-level table constraint would require a table lookup on every insert, for example). Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---