diff --git a/README.md b/README.md index 68cc982..79e154c 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,9 @@ assert results == [queryset1.count(), list(queryset2), ...] - Find a better package name? 😂 (think SEO) - Add a diagram in README depicting the time saved during network trips - Anything else which makes this better, open to ideas +- Better readable way of accessing results (instead of `results[0]`, `results[1]`) +- `FirstWrapper`, `LastWrapper` etc for lazy evaluating `.first()` and `.last()` +- MySQL support as an experiment ## Notes diff --git a/django_querysets_single_query_fetch/service.py b/django_querysets_single_query_fetch/service.py index e908a98..17d49ba 100644 --- a/django_querysets_single_query_fetch/service.py +++ b/django_querysets_single_query_fetch/service.py @@ -251,7 +251,8 @@ def _get_instances_from_results_for_model_iterable( for row_dict in results: for key, value in row_dict.items(): - if isinstance(value, dict): + # both dict and list can be possible values for json field + if isinstance(value, dict) or isinstance(value, list): row_dict[key] = json.dumps(value) else: pass diff --git a/setup.py b/setup.py index 05a01de..0544466 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setuptools.setup( name="django_querysets_single_query_fetch", - version="0.0.7", + version="0.0.8", description="Execute multiple Django querysets in a single SQL query", long_description="", author="Nishant Singh", diff --git a/testapp/migrations/0002_storeproduct_meta.py b/testapp/migrations/0002_storeproduct_meta.py new file mode 100644 index 0000000..85fc152 --- /dev/null +++ b/testapp/migrations/0002_storeproduct_meta.py @@ -0,0 +1,17 @@ +# Generated by Django 4.0 on 2024-07-03 17:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("testapp", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="storeproduct", + name="meta", + field=models.JSONField(default=dict), + ), + ] diff --git a/testapp/models.py b/testapp/models.py index cc19fcd..c1fa23a 100644 --- a/testapp/models.py +++ b/testapp/models.py @@ -19,3 +19,4 @@ class StoreProduct(models.Model): StoreProductCategory, on_delete=models.CASCADE, null=True ) selling_price = models.DecimalField(max_digits=10, decimal_places=2) + meta = models.JSONField(default=dict) diff --git a/testapp/tests/test_behaviour_for_postgres.py b/testapp/tests/test_behaviour_for_postgres.py index a42e6f6..cde452a 100644 --- a/testapp/tests/test_behaviour_for_postgres.py +++ b/testapp/tests/test_behaviour_for_postgres.py @@ -192,3 +192,31 @@ def test_search_by_datetime_will_work(self): ).execute()[0] self.assertEqual(len(stores), 1) self.assertEqual(self.store.id, stores[0].id) + + def test_query_on_json_field_with_dict_data(self): + # postgres json field need not be a dict in python, + # it can be a list as well + + # update one of the products to have a normal dict in json field + StoreProduct.objects.filter(id=self.product_1.id).update(meta={"foo": "bar"}) + + products = QuerysetsSingleQueryFetch( + querysets=[StoreProduct.objects.filter().order_by("id")] + ).execute()[0] + self.assertEqual(len(products), 2) + self.assertEqual(products[0].meta, {"foo": "bar"}) + self.assertEqual(products[1].meta, {}) + + def test_query_on_json_field_with_list_data(self): + # postgres json field need not be a dict in python, + # it can be a list as well + + # update one of the products to have a list in json field + StoreProduct.objects.filter(id=self.product_1.id).update(meta=[1, 2]) + + products = QuerysetsSingleQueryFetch( + querysets=[StoreProduct.objects.filter().order_by("id")] + ).execute()[0] + self.assertEqual(len(products), 2) + self.assertEqual(products[0].meta, [1, 2]) # product 1 + self.assertEqual(products[1].meta, {})