Explorar o código

HUE-8530 [organization] Make ORM filter chaining backward compatible

Can support username field in a values_list after the filter():
e.g.

User.objects.filter(groups__in=Group.objects.all()).values_list('username', flat=True)
Romain %!s(int64=5) %!d(string=hai) anos
pai
achega
2fec2c033c

+ 5 - 0
apps/useradmin/src/useradmin/models2.py

@@ -142,6 +142,11 @@ class UserManager(BaseUserManager):
 
     return super(UserManager, self).order_by(*args, **kwargs)
 
+  def filter(self, *args, **kwargs):
+    f = super(UserManager, self).filter(*args, **kwargs)
+    f.values_list = self.values_list  # Patch so that chaining after a filter is backward compatible
+    return f
+
   def values_list(self, *args, **kwargs):
     if 'username' in args:
       args = list(args)

+ 1 - 0
apps/useradmin/src/useradmin/models2_tests.py

@@ -130,6 +130,7 @@ class TestOrganizationSingleUser(unittest.TestCase):
     User.objects.filter(groups__in=Group.objects.all()).order_by('username')
 
     User.objects.values_list('username', flat=True)
+    User.objects.filter(groups__in=Group.objects.all()).values_list('username', flat=True)
 
     self.client2.get('/useradmin/groups/edit/default')