From 90ec66f38014a0affb04ca23b1fe269d8448a1eb Mon Sep 17 00:00:00 2001 From: M1ha Date: Thu, 7 Feb 2019 11:41:20 +0500 Subject: [PATCH] Changed model_to_dict, so it doesn't require _meta attribute, if fields are given --- src/django_clickhouse/serializers.py | 2 +- src/django_clickhouse/utils.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/django_clickhouse/serializers.py b/src/django_clickhouse/serializers.py index dc32901..bfb2a76 100644 --- a/src/django_clickhouse/serializers.py +++ b/src/django_clickhouse/serializers.py @@ -3,7 +3,7 @@ from typing import NamedTuple import pytz from django.db.models import Model as DjangoModel -from django_clickhouse.utils import model_to_dict +from .utils import model_to_dict class Django2ClickHouseModelSerializer: diff --git a/src/django_clickhouse/utils.py b/src/django_clickhouse/utils.py index ff26c92..0ee57ec 100644 --- a/src/django_clickhouse/utils.py +++ b/src/django_clickhouse/utils.py @@ -116,8 +116,9 @@ def model_to_dict(instance, fields=None, exclude_fields=None): """ data = {} - opts = instance._meta - fields = fields or {f.name for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many)} + if not fields: + opts = instance._meta + fields = {f.name for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many)} for name in set(fields) - set(exclude_fields or set()): val = getattr(instance, name, None)