{"id":14483335,"url":"https://github.com/simonw/django-queryset-transform","last_synced_at":"2025-04-15T16:37:53.332Z","repository":{"id":66508057,"uuid":"466800","full_name":"simonw/django-queryset-transform","owner":"simonw","description":"Experimental .transform(fn) method for Django QuerySets, for clever lazily evaluated optimisations.","archived":false,"fork":false,"pushed_at":"2010-05-26T10:21:37.000Z","size":91,"stargazers_count":143,"open_issues_count":1,"forks_count":11,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-18T07:53:41.606Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simonw.png","metadata":{"files":{"readme":"README.txt","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-01-10T23:42:31.000Z","updated_at":"2024-04-10T11:04:43.000Z","dependencies_parsed_at":"2023-02-20T05:31:00.240Z","dependency_job_id":null,"html_url":"https://github.com/simonw/django-queryset-transform","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fdjango-queryset-transform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fdjango-queryset-transform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fdjango-queryset-transform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonw%2Fdjango-queryset-transform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonw","download_url":"https://codeload.github.com/simonw/django-queryset-transform/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249109618,"owners_count":21214180,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-03T00:01:42.278Z","updated_at":"2025-04-15T16:37:53.304Z","avatar_url":"https://github.com/simonw.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"django_queryset_transform\n=========================\n\nAllows you to register a transforming map function with a Django QuerySet \nthat will be executed only when the QuerySet itself has been evaluated.\n\nThis allows you to build optimisations like \"fetch all tags for these 10 rows\"\nwhile still benefiting from Django's lazy QuerySet evaluation.\n\nFor example:\n    \n    def lookup_tags(item_qs):\n        item_pks = [item.pk for item in item_qs]\n        m2mfield = Item._meta.get_field_by_name('tags')[0]\n        tags_for_item = Tag.objects.filter(\n            item__in = item_pks\n        ).extra(select = {\n            'item_id': '%s.%s' % (\n                m2mfield.m2m_db_table(), m2mfield.m2m_column_name()\n            )\n        })\n        tag_dict = {}\n        for tag in tags_for_item:\n            tag_dict.setdefault(tag.item_id, []).append(tag)\n        for item in item_qs:\n            item.fetched_tags = tag_dict.get(item.pk, [])\n    \n    qs = Item.objects.filter(name__contains = 'e').transform(lookup_tags)\n    \n    for item in qs:\n        print item, item.fetched_tags\n\nPrints:\n\n    Winter comes to Ogglesbrook [\u003csledging\u003e, \u003csnow\u003e, \u003cwinter\u003e, \u003cskating\u003e]\n    Summer now [\u003cskating\u003e, \u003csunny\u003e]\n\nBut only executes two SQL queries - one to fetch the items, and one to fetch ALL of the tags for those items.\n\nSince the transformer function can transform an evaluated QuerySet, it \ndoesn't need to make extra database calls at all - it should work for things \nlike looking up additional data from a cache.multi_get() as well.\n\nOriginally inspired by http://github.com/lilspikey/django-batch-select/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonw%2Fdjango-queryset-transform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonw%2Fdjango-queryset-transform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonw%2Fdjango-queryset-transform/lists"}