Skip to content
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

初步解决 'AutoField' object has no attribute 'rel' 问题 #8

Open
Deali-Axy opened this issue Jun 8, 2018 · 3 comments
Open

初步解决 'AutoField' object has no attribute 'rel' 问题 #8

Deali-Axy opened this issue Jun 8, 2018 · 3 comments

Comments

@Deali-Axy
Copy link

修改了 Serializer 文件的data_inspect函数。
代码如下,加了一个rel属性判断。
由于代码没有注释,一时之间也找不到具体问题在哪,所以胡乱改了一下,希望作者看到问题后进行修复。

    def data_inspect(self, data, extra=None):
        if isinstance(data, (QuerySet, Page, list)):
            convert_data = []
            if extra:
                for i, obj in enumerate(data):
                    convert_data.append(self.data_inspect(obj, extra.get(
                        **{self.through_fields[0]: obj, self.through_fields[1]: self.source_field})))
            else:
                for obj in data:
                    convert_data.append(self.data_inspect(obj))
            return convert_data
        elif isinstance(data, models.Model):
            obj_dict = {}
            concrete_model = data._meta.concrete_model
            for field in concrete_model._meta.local_fields:
                # 检查 field 是否存在 rel 这个属性,为'AutoField' object has no attribute 'rel'错误填坑
                if hasattr(field, 'rel'):
                    if field.rel is None:
                        if self.check_attr(field.name) and hasattr(data, field.name):
                            obj_dict[field.name] = self.data_inspect(getattr(data, field.name))
                    else:
                        if self.check_attr(field.name) and self.foreign:
                            obj_dict[field.name] = self.data_inspect(getattr(data, field.name))
                else:
                    if self.check_attr(field.name) and hasattr(data, field.name):
                        obj_dict[field.name] = self.data_inspect(getattr(data, field.name))
            for field in concrete_model._meta.many_to_many:
                if self.check_attr(field.name) and self.many:
                    obj_dict[field.name] = self.data_inspect(getattr(data, field.name))
            for k, v in data.__dict__.items():
                if not str(k).startswith('_') and k not in obj_dict.keys() and self.check_attr(k):
                    obj_dict[k] = self.data_inspect(v)
            if extra:
                for field in extra._meta.concrete_model._meta.local_fields:
                    if field.name not in obj_dict.keys() and field.name not in self.through_fields:
                        if field.rel is None:
                            if self.check_attr(field.name) and hasattr(extra, field.name):
                                obj_dict[field.name] = self.data_inspect(getattr(extra, field.name))
                        else:
                            if self.check_attr(field.name) and self.foreign:
                                obj_dict[field.name] = self.data_inspect(getattr(extra, field.name))
            return obj_dict
        elif isinstance(data, manager.Manager):
            through_list = data.through._meta.concrete_model._meta.local_fields
            through_data = data.through._default_manager
            self.through_fields = [data.target_field.name, data.source_field.name]
            self.source_field = data.instance
            if len(through_list) > 3 and self.through:
                return self.data_inspect(data.all(), through_data)
            else:
                return self.data_inspect(data.all())
        elif isinstance(data, (datetime.datetime, datetime.date, datetime.time)):
            return self.time_func(data)
        elif isinstance(data, (ImageFieldFile, FileField)):
            return data.url if data.url else data.path
        elif isinstance(data, Decimal):
            return float(data)
        elif isinstance(data, dict):
            obj_dict = {}
            if self._dict_check:
                for k, v in data.items():
                    obj_dict[k] = self.data_inspect(v)
            else:
                for k, v in data.items():
                    if self.check_attr(k):
                        obj_dict[k] = self.data_inspect(v)
            return obj_dict
        elif isinstance(data, (str, bool, float, int)):
            return data
        else:
            return None
@Deali-Axy
Copy link
Author

我的fork版本中已经修复了这个问题:https://github.com/Deali-Axy/django-simple-serializer

@eluotao
Copy link

eluotao commented May 5, 2021

我的叉子版本中已经修复了这个问题:https : //github.com/Deali-Axy/django-simple-serializer

如何使用你这个临时版本呢,pip 无法找到

@Deali-Axy
Copy link
Author

我的叉子版本中已经修复了这个问题:https : //github.com/Deali-Axy/django-simple-serializer

如何使用你这个临时版本呢,pip 无法找到

clone下来直接import就行啦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants