thirdparty/google_appengine/lib/django/tests/modeltests/m2m_recursive/models.py
changeset 2866 a04b1e4126c4
parent 2864 2e0b0af889be
child 2868 9f7f269383f7
equal deleted inserted replaced
2864:2e0b0af889be 2866:a04b1e4126c4
     1 """
       
     2 28. Many-to-many relationships between the same two tables
       
     3 
       
     4 In this example, A Person can have many friends, who are also people. Friendship is a
       
     5 symmetrical relationship - if I am your friend, you are my friend.
       
     6 
       
     7 A person can also have many idols - but while I may idolize you, you may not think
       
     8 the same of me. 'Idols' is an example of a non-symmetrical m2m field. Only recursive
       
     9 m2m fields may be non-symmetrical, and they are symmetrical by default.
       
    10 
       
    11 This test validates that the m2m table will create a mangled name for the m2m table if
       
    12 there will be a clash, and tests that symmetry is preserved where appropriate.
       
    13 """
       
    14 
       
    15 from django.db import models
       
    16 
       
    17 class Person(models.Model):
       
    18     name = models.CharField(maxlength=20)
       
    19     friends = models.ManyToManyField('self')
       
    20     idols = models.ManyToManyField('self', symmetrical=False, related_name='stalkers')
       
    21 
       
    22     def __str__(self):
       
    23         return self.name
       
    24 
       
    25 __test__ = {'API_TESTS':"""
       
    26 >>> a = Person(name='Anne')
       
    27 >>> a.save()
       
    28 >>> b = Person(name='Bill')
       
    29 >>> b.save()
       
    30 >>> c = Person(name='Chuck')
       
    31 >>> c.save()
       
    32 >>> d = Person(name='David')
       
    33 >>> d.save()
       
    34 
       
    35 # Add some friends in the direction of field definition
       
    36 # Anne is friends with Bill and Chuck
       
    37 >>> a.friends.add(b,c)
       
    38 
       
    39 # David is friends with Anne and Chuck - add in reverse direction
       
    40 >>> d.friends.add(a,c)
       
    41 
       
    42 # Who is friends with Anne?
       
    43 >>> a.friends.all()
       
    44 [<Person: Bill>, <Person: Chuck>, <Person: David>]
       
    45 
       
    46 # Who is friends with Bill?
       
    47 >>> b.friends.all()
       
    48 [<Person: Anne>]
       
    49 
       
    50 # Who is friends with Chuck?
       
    51 >>> c.friends.all()
       
    52 [<Person: Anne>, <Person: David>]
       
    53 
       
    54 # Who is friends with David?
       
    55 >>> d.friends.all()
       
    56 [<Person: Anne>, <Person: Chuck>]
       
    57 
       
    58 # Bill is already friends with Anne - add Anne again, but in the reverse direction
       
    59 >>> b.friends.add(a)
       
    60 
       
    61 # Who is friends with Anne?
       
    62 >>> a.friends.all()
       
    63 [<Person: Bill>, <Person: Chuck>, <Person: David>]
       
    64 
       
    65 # Who is friends with Bill?
       
    66 >>> b.friends.all()
       
    67 [<Person: Anne>]
       
    68 
       
    69 # Remove Anne from Bill's friends
       
    70 >>> b.friends.remove(a)
       
    71 
       
    72 # Who is friends with Anne?
       
    73 >>> a.friends.all()
       
    74 [<Person: Chuck>, <Person: David>]
       
    75 
       
    76 # Who is friends with Bill?
       
    77 >>> b.friends.all()
       
    78 []
       
    79 
       
    80 # Clear Anne's group of friends
       
    81 >>> a.friends.clear()
       
    82 
       
    83 # Who is friends with Anne?
       
    84 >>> a.friends.all()
       
    85 []
       
    86 
       
    87 # Reverse relationships should also be gone
       
    88 # Who is friends with Chuck?
       
    89 >>> c.friends.all()
       
    90 [<Person: David>]
       
    91 
       
    92 # Who is friends with David?
       
    93 >>> d.friends.all()
       
    94 [<Person: Chuck>]
       
    95 
       
    96 
       
    97 # Add some idols in the direction of field definition
       
    98 # Anne idolizes Bill and Chuck
       
    99 >>> a.idols.add(b,c)
       
   100 
       
   101 # Bill idolizes Anne right back
       
   102 >>> b.idols.add(a)
       
   103 
       
   104 # David is idolized by Anne and Chuck - add in reverse direction
       
   105 >>> d.stalkers.add(a,c)
       
   106 
       
   107 # Who are Anne's idols?
       
   108 >>> a.idols.all()
       
   109 [<Person: Bill>, <Person: Chuck>, <Person: David>]
       
   110 
       
   111 # Who is stalking Anne?
       
   112 >>> a.stalkers.all()
       
   113 [<Person: Bill>]
       
   114 
       
   115 # Who are Bill's idols?
       
   116 >>> b.idols.all()
       
   117 [<Person: Anne>]
       
   118 
       
   119 # Who is stalking Bill?
       
   120 >>> b.stalkers.all()
       
   121 [<Person: Anne>]
       
   122 
       
   123 # Who are Chuck's idols?
       
   124 >>> c.idols.all()
       
   125 [<Person: David>]
       
   126 
       
   127 # Who is stalking Chuck?
       
   128 >>> c.stalkers.all()
       
   129 [<Person: Anne>]
       
   130 
       
   131 # Who are David's idols?
       
   132 >>> d.idols.all()
       
   133 []
       
   134 
       
   135 # Who is stalking David
       
   136 >>> d.stalkers.all()
       
   137 [<Person: Anne>, <Person: Chuck>]
       
   138 
       
   139 # Bill is already being stalked by Anne - add Anne again, but in the reverse direction
       
   140 >>> b.stalkers.add(a)
       
   141 
       
   142 # Who are Anne's idols?
       
   143 >>> a.idols.all()
       
   144 [<Person: Bill>, <Person: Chuck>, <Person: David>]
       
   145 
       
   146 # Who is stalking Anne?
       
   147 [<Person: Bill>]
       
   148 
       
   149 # Who are Bill's idols
       
   150 >>> b.idols.all()
       
   151 [<Person: Anne>]
       
   152 
       
   153 # Who is stalking Bill?
       
   154 >>> b.stalkers.all()
       
   155 [<Person: Anne>]
       
   156 
       
   157 # Remove Anne from Bill's list of stalkers
       
   158 >>> b.stalkers.remove(a)
       
   159 
       
   160 # Who are Anne's idols?
       
   161 >>> a.idols.all()
       
   162 [<Person: Chuck>, <Person: David>]
       
   163 
       
   164 # Who is stalking Anne?
       
   165 >>> a.stalkers.all()
       
   166 [<Person: Bill>]
       
   167 
       
   168 # Who are Bill's idols?
       
   169 >>> b.idols.all()
       
   170 [<Person: Anne>]
       
   171 
       
   172 # Who is stalking Bill?
       
   173 >>> b.stalkers.all()
       
   174 []
       
   175 
       
   176 # Clear Anne's group of idols
       
   177 >>> a.idols.clear()
       
   178 
       
   179 # Who are Anne's idols
       
   180 >>> a.idols.all()
       
   181 []
       
   182 
       
   183 # Reverse relationships should also be gone
       
   184 # Who is stalking Chuck?
       
   185 >>> c.stalkers.all()
       
   186 []
       
   187 
       
   188 # Who is friends with David?
       
   189 >>> d.stalkers.all()
       
   190 [<Person: Chuck>]
       
   191 
       
   192 """}