diff --git a/.travis.yml b/.travis.yml index 83874d8..a7c22fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ python: env: - DJANGO=1.6.1 DB=postgres install: + - python setup.py install - pip install -q Django==$DJANGO - pip install -r requirements.txt before_script: diff --git a/README.md b/README.md index c38ffb8..6bd9157 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ An overview of each util is below with links to more in-depth documentation and - [get_or_none](#get_or_none): Performs a get on a queryset and returns None if the object does not exist. - [upsert](#upsert): Performs an upsert (update or insert) to a model. - [bulk_update](#bulk_update): Bulk updates a list of models and the fields that have been updated. +- [id_dict](#id_dict): Returns a dictionary of objects keyed on their ID. - [post_bulk_operation](#post_bulk_operation): A signal that is fired when a bulk operation happens. ## single() @@ -130,6 +131,20 @@ Performs an bulk update on an list of objects. Any fields listed in the fields_t print model_obj2.int_field, model_obj2.float_field 10, 20.0 +## id_dict() +Returns a dictionary of the model objects keyed on their ID. + +**Examples** + + TestModel.objects.create(int_field=1) + TestModel.objects.create(int_field=2) + + print TestModel.objects.id_dict() + {1: , 2: } + + print TestModel.objects.filter(int_field=2).id_dict() + {2: } + ## post_bulk_operation(providing_args=['model']) A signal that is emitted at the end of a bulk operation. The current bulk operations are Django's update and bulk_create methods and this package's bulk_update method. The signal provides the model that was updated. diff --git a/manager_utils/VERSION b/manager_utils/VERSION index 3b04cfb..be58634 100644 --- a/manager_utils/VERSION +++ b/manager_utils/VERSION @@ -1 +1 @@ -0.2 +0.3 diff --git a/manager_utils/manager_utils.py b/manager_utils/manager_utils.py index b0cc543..f79f950 100644 --- a/manager_utils/manager_utils.py +++ b/manager_utils/manager_utils.py @@ -14,6 +14,12 @@ class ManagerUtilsQuerySet(QuerySet): """ Defines the methods in the manager utils that can also be applied to querysets. """ + def id_dict(self): + """ + Returns a dictionary of all the objects keyed on their id. + """ + return {obj.id: obj for obj in self} + def get_or_none(self, **query_params): """ Get an object or return None if it doesn't exist. @@ -51,6 +57,23 @@ class ManagerUtilsMixin(object): def get_queryset(self): return ManagerUtilsQuerySet(self.model) + def id_dict(self): + """ + Returns a dictionary of all the objects keyed on their ID. + + Returns: + A dictionary of objects from the queryset or manager that is keyed on the objects' IDs. + + Examples: + + TestModel.objects.create(int_field=1) + TestModel.objects.create(int_field=2) + + print TestModel.objects.id_dict() + + """ + return self.get_queryset().id_dict() + def bulk_create(self, objs, batch_size=None): """ Overrides Django's bulk_create function to emit a post_bulk_operation signal when bulk_create diff --git a/setup.py b/setup.py index 3f0d42a..8110121 100644 --- a/setup.py +++ b/setup.py @@ -20,10 +20,11 @@ 'Framework :: Django', ], dependency_links=[ - 'http://github.com/wesokes/django-query-builder/tarball/master#egg=django-query-builder-0.5.2', + 'http://github.com/wesokes/django-query-builder/tarball/0.5#egg=django-query-builder-0.5.2', ], install_requires=[ 'django>=1.6', + 'django-query-builder>=0.5.2', ], include_package_data=True, ) diff --git a/test_project/tests/manager_utils_tests.py b/test_project/tests/manager_utils_tests.py index 08a4461..8d5c144 100644 --- a/test_project/tests/manager_utils_tests.py +++ b/test_project/tests/manager_utils_tests.py @@ -77,6 +77,41 @@ def test_save_doesnt_emit_signal(self): self.assertEquals(self.signal_handler.num_times_called, 0) +class IdDictTest(TestCase): + """ + Tests the id_dict function. + """ + def test_no_objects_manager(self): + """ + Tests the output when no objects are present in the manager. + """ + self.assertEquals(TestModel.objects.id_dict(), {}) + + def test_objects_manager(self): + """ + Tests retrieving a dict of objects keyed on their ID from the manager. + """ + model_obj1 = G(TestModel, int_field=1) + model_obj2 = G(TestModel, int_field=2) + self.assertEquals(TestModel.objects.id_dict(), {model_obj1.id: model_obj1, model_obj2.id: model_obj2}) + + def test_no_objects_queryset(self): + """ + Tests the case when no objects are returned via a queryset. + """ + G(TestModel, int_field=1) + G(TestModel, int_field=2) + self.assertEquals(TestModel.objects.filter(int_field__gte=3).id_dict(), {}) + + def test_objects_queryset(self): + """ + Tests the case when objects are returned via a queryset. + """ + G(TestModel, int_field=1) + model_obj = G(TestModel, int_field=2) + self.assertEquals(TestModel.objects.filter(int_field__gte=2).id_dict(), {model_obj.id: model_obj}) + + class GetOrNoneTests(TestCase): """ Tests the get_or_none function in the manager utils