diff --git a/django_elasticsearch_dsl/fields.py b/django_elasticsearch_dsl/fields.py index e3e45fc7..1b091571 100644 --- a/django_elasticsearch_dsl/fields.py +++ b/django_elasticsearch_dsl/fields.py @@ -116,6 +116,11 @@ def _get_inner_field_data(self, obj, field_value_to_ignore=None): obj, field_value_to_ignore ) + # This allows for ObjectFields to be indexed from dicts with + # dynamic keys (i.e. keys/fields not defined in 'properties') + if not data and obj and isinstance(obj, dict): + data = obj + return data def get_value_from_instance(self, instance, field_value_to_ignore=None): @@ -130,7 +135,9 @@ def get_value_from_instance(self, instance, field_value_to_ignore=None): except TypeError: is_iterable = False - if is_iterable: + # While dicts are iterable, they need to be excluded here so + # their full data is indexed + if is_iterable and not isinstance(objs, dict): return [ self._get_inner_field_data(obj, field_value_to_ignore) for obj in objs if obj != field_value_to_ignore diff --git a/tests/test_fields.py b/tests/test_fields.py index af216ae2..1e2d85af 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -100,6 +100,36 @@ def test_get_value_from_instance(self): 'last_name': "bar", }) + def test_get_value_from_instance_with_partial_properties(self): + field = ObjectField( + attr='person', + properties={ + 'first_name': TextField(analyzer='foo') + } + ) + + instance = NonCallableMock( + person=NonCallableMock(first_name='foo', last_name='bar') + ) + + self.assertEqual(field.get_value_from_instance(instance), { + 'first_name': "foo" + }) + + def test_get_value_from_instance_without_properties(self): + field = ObjectField(attr='person') + + instance = NonCallableMock( + person={'first_name': 'foo', 'last_name': 'bar'} + ) + + self.assertEqual(field.get_value_from_instance(instance), + { + 'first_name': "foo", + 'last_name': "bar" + } + ) + def test_get_value_from_instance_with_inner_objectfield(self): field = ObjectField(attr='person', properties={ 'first_name': TextField(analyzer='foo'), @@ -120,6 +150,30 @@ def test_get_value_from_instance_with_inner_objectfield(self): 'additional': {'age': 12} }) + def test_get_value_from_instance_with_inner_objectfield_without_properties(self): + field = ObjectField( + attr='person', + properties={ + 'first_name': TextField(analyzer='foo'), + 'last_name': TextField(), + 'additional': ObjectField() + } + ) + + instance = NonCallableMock(person=NonCallableMock( + first_name="foo", + last_name="bar", + additional={'age': 12} + )) + + self.assertEqual(field.get_value_from_instance(instance), + { + 'first_name': "foo", + 'last_name': "bar", + 'additional': {'age': 12} + } + ) + def test_get_value_from_instance_with_none_inner_objectfield(self): field = ObjectField(attr='person', properties={ 'first_name': TextField(analyzer='foo'), @@ -168,6 +222,29 @@ def test_get_value_from_iterable(self): } ]) + def test_get_value_from_iterable_without_properties(self): + field = ObjectField(attr='person') + + instance = NonCallableMock( + person=[ + {'first_name': "foo1", 'last_name': "bar1"}, + {'first_name': "foo2", 'last_name': "bar2"} + ] + ) + + self.assertEqual(field.get_value_from_instance(instance), + [ + { + 'first_name': "foo1", + 'last_name': "bar1", + }, + { + 'first_name': "foo2", + 'last_name': "bar2", + } + ] + ) + class NestedFieldTestCase(TestCase): def test_get_mapping(self):