zoukankan      html  css  js  c++  java
  • AutoField的话就报错:'AutoField' object has no attribute '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
  • 相关阅读:
    input表单元素的默认padding不一致问题
    【转】iOS25彩票 幸运转盘
    微分起源
    转载--微分几何为何必然兴起?
    前缀和?or差分序列?
    noip2014 小结
    2019艾瑞(北京)年度高峰会议-数能驱动新变量
    MSF初体验
    s实现指定时间自动跳转到某个页面
    css实现居中
  • 原文地址:https://www.cnblogs.com/ameile/p/10078809.html
Copyright © 2011-2022 走看看