2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2007-03-24 04:17:04 +08:00
|
|
|
28. Many-to-many relationships between the same two tables
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
In this example, a ``Person`` can have many friends, who are also ``Person``
|
|
|
|
objects. Friendship is a symmetrical relationship - if I am your friend, you
|
|
|
|
are my friend. Here, ``friends`` is an example of a symmetrical
|
|
|
|
``ManyToManyField``.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
A ``Person`` can also have many idols - but while I may idolize you, you may
|
|
|
|
not think the same of me. Here, ``idols`` is an example of a non-symmetrical
|
|
|
|
``ManyToManyField``. Only recursive ``ManyToManyField`` fields may be
|
|
|
|
non-symmetrical, and they are symmetrical by default.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
This test validates that the many-to-many table is created using a mangled name
|
|
|
|
if there is a name clash, and tests that symmetry is preserved where
|
|
|
|
appropriate.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-10-10 00:26:48 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 09:31:56 +08:00
|
|
|
class Person(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=20)
|
2006-05-02 09:31:56 +08:00
|
|
|
friends = models.ManyToManyField('self')
|
|
|
|
idols = models.ManyToManyField('self', symmetrical=False, related_name='stalkers')
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|