Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a symmetrical method

  • Loading branch information...
commit 0d145fcd9a3fff679114dace6a6ef1a8ee64371e 1 parent cd1c4b1
@coleifer authored
Showing with 28 additions and 0 deletions.
  1. +22 −0 genericm2m/genericm2m_tests/tests.py
  2. +6 −0 genericm2m/models.py
View
22 genericm2m/genericm2m_tests/tests.py
@@ -114,6 +114,28 @@ def test_related_to(self):
(self.soda, self.pizza),
))
+ def test_symmetrical(self):
+ self.pizza.related.connect(self.soda)
+ self.pizza.related.connect(self.beer)
+ self.pizza.related.connect(self.table)
+ self.sandwich.related.connect(self.soda)
+ self.sandwich.related.connect(self.milk)
+ self.mario.related.connect(self.soda)
+ self.soda.related.connect(self.pizza)
+
+ related = self.soda.related.symmetrical().order_by('id')
+ self.assertRelatedEqual(related, (
+ (self.pizza, self.soda),
+ (self.sandwich, self.soda),
+ (self.mario, self.soda),
+ (self.soda, self.pizza),
+ ))
+
+ related = self.beer.related.symmetrical()
+ self.assertRelatedEqual(related, (
+ (self.pizza, self.beer),
+ ))
+
def test_manager_methods(self):
"""
Since the RelatedObjectsDescriptor behaves like a dynamic manager (much
View
6 genericm2m/models.py
@@ -173,6 +173,12 @@ def related_to(self):
**rel_obj.get_query_to(instance)
)
+ def symmetrical(self):
+ return superclass.get_query_set(self).filter(
+ Q(**rel_obj.get_query_from(instance)) |
+ Q(**rel_obj.get_query_to(instance))
+ ).distinct()
+
manager = RelatedManager()
manager.core_filters = core_filters
manager.model = self.related_model

1 comment on commit 0d145fc

@jpic

Really neat, thanks a heap !!

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