-
Notifications
You must be signed in to change notification settings - Fork 269
Updated ObjectField to allow for unmapped keys #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
LGTM. @oehrlein Is it possible for you to add some tests? if not possible, we can merge as it is. |
@oehrlein BTW, Thanks for your PR. I was just trying to figure out what was happening here! |
@safwanrahman Re: tests, I can take a crack at it, but not sure when I'd be able to get it done. I could potentially try to start working on it later today, but I can't make any guarantees. I think it'd be best to merge now and get tests added in later, if that's fine with you. And no problem! Looks like the main issue at hand was |
django_elasticsearch_dsl/fields.py
Outdated
@@ -125,7 +128,7 @@ def get_value_from_instance(self, instance, field_value_to_ignore=None): | |||
if objs is None: | |||
return {} | |||
try: | |||
is_iterable = bool(iter(objs)) | |||
is_iterable = bool(iter(objs)) and not isinstance(objs, dict) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think rather than changing is_iterable
, change to if is_iterable and not isinstance(objs, dict):
below.
django_elasticsearch_dsl/fields.py
Outdated
@@ -125,7 +128,7 @@ def get_value_from_instance(self, instance, field_value_to_ignore=None): | |||
if objs is None: | |||
return {} | |||
try: | |||
is_iterable = bool(iter(objs)) | |||
is_iterable = bool(iter(objs)) and not isinstance(objs, dict) | |||
except TypeError: | |||
is_iterable = False | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this.
if is_iterable and not isinstance(objs, dict):: | |
.... |
I am concerned that if anyone use |
@safwanrahman Good call on moving the iterable dict check, it makes more sense with what you suggested. I committed the change. |
@safwanrahman If there are For example, using an
Without defined properties:
With partially defined properties:
With all defined properties:
For example, using a
Without defined properties:
With partially defined properties:
With all defined properties:
|
@safwanrahman Following up on tests, I was able to add some. See commit 31c8857. |
@oehrlein That looks excellent! 🤩🚀 |
@@ -128,8 +131,8 @@ def get_value_from_instance(self, instance, field_value_to_ignore=None): | |||
is_iterable = bool(iter(objs)) | |||
except TypeError: | |||
is_iterable = False | |||
|
|||
if is_iterable: | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here why we are excluding the dictionary?
Something like
# Need to exclude dictionary, as it is iterable, but the full data need to be indexed. |
@safwanrahman No problem! I also just added in comments for the two changes! |
@safwanrahman just wanted to follow up on this - is there anything else you need or can it be merged? |
@oehrlein I am so sorry that I could merge it earlier! Was quite busy dealing with life things! 😓 |
@oehrlein I have rebased and merged it to master. Thanks a lot! 👍 💯 |
@safwanrahman No worries, I appreciate you taking the time to do this! |
I adjusted the ObjectField class to allow for data with unmapped keys. Previously, if an ObjectField (or NestedField) was created without mapping, it'd show up empty in the index.
For example:
This prevents the need for the workaround mentioned in #36, which uses a
prepare_
function such as this:This fixes issues highlighted in #13, #36, and #235.