We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Learner activity records partially not visible after upgrade to 0.14b3
# Observed Behaviour

Upgraded from 0.13.3 to 0.14b3. Learner activity records were partially not visible in Class Home -> Class activity and Reports. Downgraded back to 0.13.3 and they are all visible again.

# Expected behavior

All learner activity should be visible as prior to upgrade


# User-facing consequences

 Confusion and fear of data loss.

# Errors and logs

None on screen

# Steps to reproduce

Upgrade from 0.13.3 to 0.14.0-b3 and check learner activity records.



# Context
Ubuntu 18.04.3
Package : 0.14.beta3 .deb


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `kolibri/core/query.py`
Content:
```
1 from django.db import connection
2 from django.db.models import Aggregate
3 from django.db.models import CharField
4 from django.db.models import IntegerField
5 from django.db.models import Subquery
6 
7 try:
8     from django.contrib.postgres.aggregates import ArrayAgg
9 
10     class NotNullArrayAgg(ArrayAgg):
11         def convert_value(self, value, expression, connection, context):
12             if not value:
13                 return []
14             return filter(lambda x: x is not None, value)
15 
16 
17 except ImportError:
18     NotNullArrayAgg = None
19 
20 
21 class SQCount(Subquery):
22     # Include ALIAS at the end to support Postgres
23     template = "(SELECT COUNT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)"
24     output_field = IntegerField()
25 
26 
27 class SQSum(Subquery):
28     # Include ALIAS at the end to support Postgres
29     template = "(SELECT SUM(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)"
30     output_field = IntegerField()
31 
32 
33 class GroupConcatSubquery(Subquery):
34     template = "(SELECT GROUP_CONCAT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)"
35     output_field = CharField()
36 
37     def as_postgresql(self, compiler, connection):
38         self.template = (
39             "(SELECT STRING_AGG(%(field)s, ',') FROM (%(subquery)s) AS %(field)s__sum)"
40         )
41         return super(GroupConcatSubquery, self).as_sql(compiler, connection)
42 
43 
44 class GroupConcat(Aggregate):
45     template = "GROUP_CONCAT(%(field)s)"
46     output_field = CharField()
47 
48     def __init__(self, *args, **kwargs):
49         self.result_field = kwargs.pop("result_field", None)
50         super(GroupConcat, self).__init__(*args, **kwargs)
51 
52     def convert_value(self, value, expression, connection, context):
53         if not value:
54             return []
55         results = value.split(",")
56         if self.result_field is not None:
57             return map(self.result_field.to_python, results)
58         return results
59 
60 
61 def get_source_field(model, field_path):
62     # Get the source field from the model so that we can properly coerce values
63     # this is necessary when we are using GroupConcat to return non-string fields.
64     paths = field_path.split("__")
65     while len(paths) > 1:
66         model = model._meta.get_field(paths.pop(0)).related_model
67     return model._meta.get_field(paths[0])
68 
69 
70 def annotate_array_aggregate(queryset, **kwargs):
71     if connection.vendor == "postgresql" and NotNullArrayAgg is not None:
72         return queryset.annotate(
73             **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}
74         )
75     model = queryset.model
76     # Call values on "pk" to insert a GROUP BY to ensure the GROUP CONCAT
77     # is called by row and not across the entire queryset.
78     return queryset.values("pk").annotate(
79         **{
80             target: GroupConcat(source, result_field=get_source_field(model, source))
81             for target, source in kwargs.items()
82         }
83     )
84
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/kolibri/core/query.py b/kolibri/core/query.py
--- a/kolibri/core/query.py
+++ b/kolibri/core/query.py
@@ -8,10 +8,17 @@
     from django.contrib.postgres.aggregates import ArrayAgg
 
     class NotNullArrayAgg(ArrayAgg):
+        def __init__(self, *args, **kwargs):
+            self.result_field = kwargs.pop("result_field", None)
+            super(NotNullArrayAgg, self).__init__(*args, **kwargs)
+
         def convert_value(self, value, expression, connection, context):
             if not value:
                 return []
-            return filter(lambda x: x is not None, value)
+            results = list(filter(lambda x: x is not None, value))
+            if self.result_field is not None:
+                return list(map(self.result_field.to_python, results))
+            return results
 
 
 except ImportError:
@@ -54,7 +61,7 @@
             return []
         results = value.split(",")
         if self.result_field is not None:
-            return map(self.result_field.to_python, results)
+            return list(map(self.result_field.to_python, results))
         return results
 
 
@@ -68,11 +75,16 @@
 
 
 def annotate_array_aggregate(queryset, **kwargs):
+    model = queryset.model
     if connection.vendor == "postgresql" and NotNullArrayAgg is not None:
         return queryset.annotate(
-            **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}
+            **{
+                target: NotNullArrayAgg(
+                    source, result_field=get_source_field(model, source)
+                )
+                for target, source in kwargs.items()
+            }
         )
-    model = queryset.model
     # Call values on "pk" to insert a GROUP BY to ensure the GROUP CONCAT
     # is called by row and not across the entire queryset.
     return queryset.values("pk").annotate(

{"golden_diff": "diff --git a/kolibri/core/query.py b/kolibri/core/query.py\n--- a/kolibri/core/query.py\n+++ b/kolibri/core/query.py\n@@ -8,10 +8,17 @@\n     from django.contrib.postgres.aggregates import ArrayAgg\n \n     class NotNullArrayAgg(ArrayAgg):\n+        def __init__(self, *args, **kwargs):\n+            self.result_field = kwargs.pop(\"result_field\", None)\n+            super(NotNullArrayAgg, self).__init__(*args, **kwargs)\n+\n         def convert_value(self, value, expression, connection, context):\n             if not value:\n                 return []\n-            return filter(lambda x: x is not None, value)\n+            results = list(filter(lambda x: x is not None, value))\n+            if self.result_field is not None:\n+                return list(map(self.result_field.to_python, results))\n+            return results\n \n \n except ImportError:\n@@ -54,7 +61,7 @@\n             return []\n         results = value.split(\",\")\n         if self.result_field is not None:\n-            return map(self.result_field.to_python, results)\n+            return list(map(self.result_field.to_python, results))\n         return results\n \n \n@@ -68,11 +75,16 @@\n \n \n def annotate_array_aggregate(queryset, **kwargs):\n+    model = queryset.model\n     if connection.vendor == \"postgresql\" and NotNullArrayAgg is not None:\n         return queryset.annotate(\n-            **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}\n+            **{\n+                target: NotNullArrayAgg(\n+                    source, result_field=get_source_field(model, source)\n+                )\n+                for target, source in kwargs.items()\n+            }\n         )\n-    model = queryset.model\n     # Call values on \"pk\" to insert a GROUP BY to ensure the GROUP CONCAT\n     # is called by row and not across the entire queryset.\n     return queryset.values(\"pk\").annotate(\n", "issue": "Learner activity records partially not visible after upgrade to 0.14b3\n# Observed Behaviour\n\nUpgraded from 0.13.3 to 0.14b3. Learner activity records were partially not visible in Class Home -> Class activity and Reports. Downgraded back to 0.13.3 and they are all visible again.\n\n# Expected behavior\n\nAll learner activity should be visible as prior to upgrade\n\n\n# User-facing consequences\n\n Confusion and fear of data loss.\n\n# Errors and logs\n\nNone on screen\n\n# Steps to reproduce\n\nUpgrade from 0.13.3 to 0.14.0-b3 and check learner activity records.\n\n\n\n# Context\nUbuntu 18.04.3\nPackage : 0.14.beta3 .deb\n\n", "before_files": [{"content": "from django.db import connection\nfrom django.db.models import Aggregate\nfrom django.db.models import CharField\nfrom django.db.models import IntegerField\nfrom django.db.models import Subquery\n\ntry:\n    from django.contrib.postgres.aggregates import ArrayAgg\n\n    class NotNullArrayAgg(ArrayAgg):\n        def convert_value(self, value, expression, connection, context):\n            if not value:\n                return []\n            return filter(lambda x: x is not None, value)\n\n\nexcept ImportError:\n    NotNullArrayAgg = None\n\n\nclass SQCount(Subquery):\n    # Include ALIAS at the end to support Postgres\n    template = \"(SELECT COUNT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = IntegerField()\n\n\nclass SQSum(Subquery):\n    # Include ALIAS at the end to support Postgres\n    template = \"(SELECT SUM(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = IntegerField()\n\n\nclass GroupConcatSubquery(Subquery):\n    template = \"(SELECT GROUP_CONCAT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = CharField()\n\n    def as_postgresql(self, compiler, connection):\n        self.template = (\n            \"(SELECT STRING_AGG(%(field)s, ',') FROM (%(subquery)s) AS %(field)s__sum)\"\n        )\n        return super(GroupConcatSubquery, self).as_sql(compiler, connection)\n\n\nclass GroupConcat(Aggregate):\n    template = \"GROUP_CONCAT(%(field)s)\"\n    output_field = CharField()\n\n    def __init__(self, *args, **kwargs):\n        self.result_field = kwargs.pop(\"result_field\", None)\n        super(GroupConcat, self).__init__(*args, **kwargs)\n\n    def convert_value(self, value, expression, connection, context):\n        if not value:\n            return []\n        results = value.split(\",\")\n        if self.result_field is not None:\n            return map(self.result_field.to_python, results)\n        return results\n\n\ndef get_source_field(model, field_path):\n    # Get the source field from the model so that we can properly coerce values\n    # this is necessary when we are using GroupConcat to return non-string fields.\n    paths = field_path.split(\"__\")\n    while len(paths) > 1:\n        model = model._meta.get_field(paths.pop(0)).related_model\n    return model._meta.get_field(paths[0])\n\n\ndef annotate_array_aggregate(queryset, **kwargs):\n    if connection.vendor == \"postgresql\" and NotNullArrayAgg is not None:\n        return queryset.annotate(\n            **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}\n        )\n    model = queryset.model\n    # Call values on \"pk\" to insert a GROUP BY to ensure the GROUP CONCAT\n    # is called by row and not across the entire queryset.\n    return queryset.values(\"pk\").annotate(\n        **{\n            target: GroupConcat(source, result_field=get_source_field(model, source))\n            for target, source in kwargs.items()\n        }\n    )\n", "path": "kolibri/core/query.py"}], "after_files": [{"content": "from django.db import connection\nfrom django.db.models import Aggregate\nfrom django.db.models import CharField\nfrom django.db.models import IntegerField\nfrom django.db.models import Subquery\n\ntry:\n    from django.contrib.postgres.aggregates import ArrayAgg\n\n    class NotNullArrayAgg(ArrayAgg):\n        def __init__(self, *args, **kwargs):\n            self.result_field = kwargs.pop(\"result_field\", None)\n            super(NotNullArrayAgg, self).__init__(*args, **kwargs)\n\n        def convert_value(self, value, expression, connection, context):\n            if not value:\n                return []\n            results = list(filter(lambda x: x is not None, value))\n            if self.result_field is not None:\n                return list(map(self.result_field.to_python, results))\n            return results\n\n\nexcept ImportError:\n    NotNullArrayAgg = None\n\n\nclass SQCount(Subquery):\n    # Include ALIAS at the end to support Postgres\n    template = \"(SELECT COUNT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = IntegerField()\n\n\nclass SQSum(Subquery):\n    # Include ALIAS at the end to support Postgres\n    template = \"(SELECT SUM(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = IntegerField()\n\n\nclass GroupConcatSubquery(Subquery):\n    template = \"(SELECT GROUP_CONCAT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = CharField()\n\n    def as_postgresql(self, compiler, connection):\n        self.template = (\n            \"(SELECT STRING_AGG(%(field)s, ',') FROM (%(subquery)s) AS %(field)s__sum)\"\n        )\n        return super(GroupConcatSubquery, self).as_sql(compiler, connection)\n\n\nclass GroupConcat(Aggregate):\n    template = \"GROUP_CONCAT(%(field)s)\"\n    output_field = CharField()\n\n    def __init__(self, *args, **kwargs):\n        self.result_field = kwargs.pop(\"result_field\", None)\n        super(GroupConcat, self).__init__(*args, **kwargs)\n\n    def convert_value(self, value, expression, connection, context):\n        if not value:\n            return []\n        results = value.split(\",\")\n        if self.result_field is not None:\n            return list(map(self.result_field.to_python, results))\n        return results\n\n\ndef get_source_field(model, field_path):\n    # Get the source field from the model so that we can properly coerce values\n    # this is necessary when we are using GroupConcat to return non-string fields.\n    paths = field_path.split(\"__\")\n    while len(paths) > 1:\n        model = model._meta.get_field(paths.pop(0)).related_model\n    return model._meta.get_field(paths[0])\n\n\ndef annotate_array_aggregate(queryset, **kwargs):\n    model = queryset.model\n    if connection.vendor == \"postgresql\" and NotNullArrayAgg is not None:\n        return queryset.annotate(\n            **{\n                target: NotNullArrayAgg(\n                    source, result_field=get_source_field(model, source)\n                )\n                for target, source in kwargs.items()\n            }\n        )\n    # Call values on \"pk\" to insert a GROUP BY to ensure the GROUP CONCAT\n    # is called by row and not across the entire queryset.\n    return queryset.values(\"pk\").annotate(\n        **{\n            target: GroupConcat(source, result_field=get_source_field(model, source))\n            for target, source in kwargs.items()\n        }\n    )\n", "path": "kolibri/core/query.py"}]}