Fixed serialization to tuple to_python converting

This commit is contained in:
M1ha 2019-02-04 13:07:45 +05:00
parent a205b451a0
commit bf69c29e4f

View File

@ -1,7 +1,8 @@
import datetime
from typing import NamedTuple from typing import NamedTuple
import pytz
from django.db.models import Model as DjangoModel from django.db.models import Model as DjangoModel
from django_clickhouse.utils import model_to_dict from django_clickhouse.utils import model_to_dict
@ -15,19 +16,16 @@ class Django2ClickHouseModelSerializer:
self.exclude_serialize_fields = exclude_fields self.exclude_serialize_fields = exclude_fields
self._result_class = self._model_cls.get_tuple_class(defaults=defaults) self._result_class = self._model_cls.get_tuple_class(defaults=defaults)
self._fields = self._model_cls.fields(writable=False)
def _get_serialize_kwargs(self, obj): def _get_serialize_kwargs(self, obj):
data = model_to_dict(obj, fields=self.serialize_fields, exclude_fields=self.exclude_serialize_fields) data = model_to_dict(obj, fields=self.serialize_fields, exclude_fields=self.exclude_serialize_fields)
# Remove None values, they should be initialized as defaults # Remove None values, they should be initialized as defaults
result = {} result = {
for key, value in data.items(): key: self._fields[key].to_python(value, pytz.utc)
if value is None: for key, value in data.items() if value is not None
pass }
elif isinstance(value, bool):
result[key] = int(value)
else:
result[key] = value
return result return result