Skip to content

Commit

Permalink
Merge pull request #9 from iNishant/feature/json-field-list-bug
Browse files Browse the repository at this point in the history
json field list bug fix
  • Loading branch information
iNishant authored Jul 3, 2024
2 parents ab3c7c7 + 1ec794b commit 734d61e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion django_querysets_single_query_fetch/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions testapp/migrations/0002_storeproduct_meta.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
1 change: 1 addition & 0 deletions testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 28 additions & 0 deletions testapp/tests/test_behaviour_for_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})

0 comments on commit 734d61e

Please sign in to comment.