博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AutoField的话就报错:'AutoField' object has no attribute 'rel'
阅读量:5058 次
发布时间:2019-06-12

本文共 3827 字,大约阅读时间需要 12 分钟。

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

 

转载于:https://www.cnblogs.com/ameile/p/10078809.html

你可能感兴趣的文章
SVN服务器搭建和使用(三)(转载)
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
【安卓5】高级控件——拖动条SeekBar
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
jdk从1.8降到jdk1.7失败
查看>>
一些关于IO流的问题
查看>>
mongo备份操作
查看>>
8 -- 深入使用Spring -- 3...1 Resource实现类InputStreamResource、ByteArrayResource
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
一个关于vue+mysql+express的全栈项目(六)------ 聊天模型的设计
查看>>
【知识库】-数据库_MySQL 的七种 join
查看>>
.net 写文件上传下载webservice
查看>>