在Django中访问Null DecimalField时的RecursionError

时间:2018-05-10 17:43:23

标签: python django python-3.x django-models

我在Django 1.11.2中有一个模型DecimalField。该字段可以为空。访问该字段时,由于某种原因我收到了递归错误。

ipdb> student.current_longitude
*** RecursionError: maximum recursion depth exceeded while getting the str of an object
ipdb>

这是否与将null值序列化为DecimalField的方式有关?这里发生了什么?

ipdb> type(student.current_longitude)
*** RecursionError: maximum recursion depth exceeded while getting the str of an object
ipdb>

模型定义

current_longitude = models.DecimalField(null=True, blank=True, decimal_places=13, max_digits=16)
current_latitude = models.DecimalField(null=True, blank=True, decimal_places=13, max_digits=16)

更新

有一个__setattr__的混合物会抛出异常:

def __setattr__(self, key, value):

    if key in ('updated_at',):
        pass

    elif getattr(self, '_initiated', False) and key not in getattr(self, '_non_model_fields', set()):
        try:
            field = self._meta.get_field(key)
        except FieldDoesNotExist:
            self._non_model_fields.add(key)
        else:
            # Foreignkeys may show up twice once as _id and once as the object
            # and for serialization and consistency purposes we only want the _id property
            # so we detect if the field is a FK and it's the FK not ending in _id and exclude it.
            # Also GeopositionField is ignoring because this field raises recursion depth via this mixin
            if isinstance(field, ForeignKey) and key[-3:] != '_id' or isinstance(field, GeopositionField):
                self._non_model_fields.add(key)
            else:
                old_value = getattr(self, key)
                if old_value != value:
                    self._dirty_fields[key] = old_value

此行old_value = getattr(self, key)上会抛出异常。为什么getattr()会调用setter?

0 个答案:

没有答案
相关问题