Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding a symmetrical method

  • Loading branch information...
commit 0d145fcd9a3fff679114dace6a6ef1a8ee64371e 1 parent cd1c4b1
Charles Leifer authored July 12, 2012
22  genericm2m/genericm2m_tests/tests.py
@@ -114,6 +114,28 @@ def test_related_to(self):
114 114
             (self.soda, self.pizza),
115 115
         ))
116 116
 
  117
+    def test_symmetrical(self):
  118
+        self.pizza.related.connect(self.soda)
  119
+        self.pizza.related.connect(self.beer)
  120
+        self.pizza.related.connect(self.table)
  121
+        self.sandwich.related.connect(self.soda)
  122
+        self.sandwich.related.connect(self.milk)
  123
+        self.mario.related.connect(self.soda)
  124
+        self.soda.related.connect(self.pizza)
  125
+
  126
+        related = self.soda.related.symmetrical().order_by('id')
  127
+        self.assertRelatedEqual(related, (
  128
+            (self.pizza, self.soda),
  129
+            (self.sandwich, self.soda),
  130
+            (self.mario, self.soda),
  131
+            (self.soda, self.pizza),
  132
+        ))
  133
+
  134
+        related = self.beer.related.symmetrical()
  135
+        self.assertRelatedEqual(related, (
  136
+            (self.pizza, self.beer),
  137
+        ))
  138
+
117 139
     def test_manager_methods(self):
118 140
         """
119 141
         Since the RelatedObjectsDescriptor behaves like a dynamic manager (much
6  genericm2m/models.py
@@ -173,6 +173,12 @@ def related_to(self):
173 173
                     **rel_obj.get_query_to(instance)
174 174
                 )
175 175
 
  176
+            def symmetrical(self):
  177
+                return superclass.get_query_set(self).filter(
  178
+                    Q(**rel_obj.get_query_from(instance)) |
  179
+                    Q(**rel_obj.get_query_to(instance))
  180
+                ).distinct()
  181
+
176 182
         manager = RelatedManager()
177 183
         manager.core_filters = core_filters
178 184
         manager.model = self.related_model

1 note on commit 0d145fc

James Pic
jpic commented on 0d145fc July 12, 2012

Really neat, thanks a heap !!

Please sign in to comment.
Something went wrong with that request. Please try again.