]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix slice addressing of _AssociationList with python3
authorGilles Dartiguelongue <gilles.dartiguelongue@esiee.org>
Tue, 9 Dec 2014 11:08:12 +0000 (12:08 +0100)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Mar 2015 18:28:50 +0000 (14:28 -0400)
lib/sqlalchemy/ext/associationproxy.py
test/ext/test_associationproxy.py

index bb08ce9ba678655836fdf4f252d32db18b10263c..c0e71068bf8f8f51aef299b513ac41551a8358d5 100644 (file)
@@ -527,7 +527,10 @@ class _AssociationList(_AssociationCollection):
         return self.setter(object, value)
 
     def __getitem__(self, index):
-        return self._get(self.col[index])
+        if not isinstance(index, slice):
+            return self._get(self.col[index])
+        else:
+            return [self._get(member) for member in self.col[index]]
 
     def __setitem__(self, index, value):
         if not isinstance(index, slice):
index 67e4747053e62e45daa2963c785a664f3e7cb15a..9e328a35fd0c8e62891164af895b42436a5a953d 100644 (file)
@@ -912,6 +912,22 @@ class LazyLoadTest(fixtures.TestBase):
         self.assert_('_children' in p.__dict__)
         self.assert_(len(p._children) == 3)
 
+    def test_slicing_list(self):
+        Parent, Child = self.Parent, self.Child
+
+        mapper(Parent, self.table, properties={
+            '_children': relationship(Child, lazy='select',
+                                  collection_class=list)})
+
+        p = Parent('p')
+        p.children = ['a', 'b', 'c']
+
+        p = self.roundtrip(p)
+
+        self.assert_(len(p._children) == 3)
+        eq_('b', p.children[1])
+        eq_(['b', 'c'], p.children[-2:])
+
     def test_lazy_scalar(self):
         Parent, Child = self.Parent, self.Child