Skip to content

Commit

Permalink
fix pointer split in list get_field
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan committed Apr 27, 2020
1 parent e7e11a8 commit f05b5c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config_field/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .serializers import ConfigSerializerMethodField

__version__ = '0.2.9'
__version__ = '0.3.0'
__author__ = 'bzdvdn'
36 changes: 27 additions & 9 deletions config_field/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ class Book(models.Model):
desc = 'desc'
class ExampleSerializer(self):
book_name = ConfigSerializerMethodField(relation_field='book', get_field='name') (return "book_name": "test")
full_data = ConfigSerializerMethodField(relation_field='book', get_field=['name', 'desc']) (return "book_name": "test desc")
book_name = ConfigSerializerMethodField(relation_field='book', get_field='name') (return 'book_name': 'test')
full_data = ConfigSerializerMethodField(relation_field='book', get_field=['name', 'desc']) (return 'book_name': 'test desc')
"""

def __init__(self, get_field=None, split_value=None, split_index=None, to_lower=False,
to_capitalize=False, to_upper=False, to_strip=False, default='none', allow_null=False, ** kwargs):
def __init__(
self,
get_field=None,
split_value=None,
split_index=None,
to_lower=False,
to_capitalize=False,
to_upper=False,
to_strip=False,
default='none',
allow_null=False,
**kwargs
):
self.get_field = get_field
self.split_value = split_value
self.split_index = split_index
Expand All @@ -39,11 +50,17 @@ def _create_dict_value(self, obj):
attr = None
if '.' in self.get_field:
return self.__split_by_pointer_value(self.get_field, obj)
if isinstance(self.get_field, list):
values = []
for field in self.get_field:
if '.' in field:
values.append(self.__split_by_pointer(field, obj))
else:
values.append(obj[field]) if obj.get('field') else None
attr = ' '.join(str(v) for v in values if v)
elif isinstance(self.get_field, str):
attr = obj.get(self.get_field)
elif isinstance(self.get_field, list):
values = [obj[field] for field in self.get_field if obj.get(field)]
attr = " ".join(str(v) for v in values if v)

if attr is None and not self.allow_null:
return self.default_value

Expand Down Expand Up @@ -82,10 +99,11 @@ def _create_model_value(self, obj):
elif isinstance(self.get_field, list):
values = [
self.__split_by_pointer_value(field, obj)
if '.' in field else getattr(obj, field, None)
if '.' in field
else getattr(obj, field, None)
for field in self.get_field
]
attr = " ".join(str(v) for v in values if v)
attr = ' '.join(str(v) for v in values if v)
if attr is None and not self.allow_null:
return self.default_value

Expand Down

0 comments on commit f05b5c6

Please sign in to comment.