为什么克隆FloatField的此子类(带有添加的验证器)会引发异常? [英] Why does cloning this subclass of FloatField (with added validators) throw an exception?

查看:102
本文介绍了为什么克隆FloatField的此子类(带有添加的验证器)会引发异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和Django的新手。

I am new to Python and Django.

我希望我的模型具有经过范围验证的浮点数。来自此答案,我这样写:

I want my model to have range-validated floats. From this answer I wrote this:

class FloatRangeField (FloatField):
    """A FloatField constrained to a given range."""

    def __init__ (self, minimum, maximum, **kwargs):

        minmax = [MinValueValidator (minimum), MaxValueValidator (maximum)]

        print ("\n\t\tFloatRangeField({},{})".format(minimum,maximum)) # (A)
        FloatField.__init__ (self, validators = minmax, **kwargs)
        print ("\t\tFINISHED\n")

python3 manage.py migration 中的错误,我将其范围缩小为 clone()调用。演示:

This was causing errors in python3 manage.py migrate, I narrowed it down to a clone() call. Demonstration:

print ("HERE 1")
tmp1 = FloatRangeField (10, 20)
print ("HERE 2")
tmp2 = FloatRangeField (10, 20)
print ("HERE 3")
tmp3 = tmp1.clone ()      # (B)
print ("HERE 4")

它从#行抛出异常(B) 。奇怪的是,发生这种情况时,打印了#(A)行中的跟踪。输出为:

It throws an exception from line # (B). Oddly, when this happens, the trace at line # (A) is not printed. Here is the output:

HERE 1

                FloatRangeField(10,20)
                FINISHED

HERE 2

                FloatRangeField(10,20)
                FINISHED

HERE 3
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()

[...snip...]

    tmp3 = tmp1.clone ()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 470, in clone
    return self.__class__(*args, **kwargs)
TypeError: __init__() missing 2 required positional arguments: 'minimum' and 'maximum'

#(A)没有打印任何内容,这与我在迁移期间看到的错误不相同 code>。如果我取出 tmp1 = ... 的东西并运行 migrate ,则回溯看起来像这样

As well as the weirdness of nothing being printed at # (A), this is not the same as the error which I saw during migrate. If I take out the tmp1=... stuff and run migrate, the traceback looks like this

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/state.py", line 411, in from_model
    fields.append((name, field.clone()))
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 470, in clone
    return self.__class__(*args, **kwargs)

[...snip...]

    FloatField.__init__ (self, validators = minmax, **kwargs)
TypeError: __init__() got multiple values for keyword argument 'validators'

为什么会这样,我应该如何实现 FloatRangeField 是否封装了最小/最大验证器?

Why is this breaking and how should I implement FloatRangeField so as to encapsulate the having of min/max validators?

推荐答案

像是用有关自定义模型字段的文档


如果添加新的关键字参数,则需要编写代码它的值变成 kwargs 自己(...)

所以您应该添加解构函数也是如此。这是必需的,例如在迁移文件中表示该字段:

So you should add a deconstructor as well. This is necessary, for example to represent this field in a migration file:

class FloatRangeField (FloatField):
    """A FloatField constrained to a given range."""

    def __init__ (self, minimum, maximum, **kwargs):
        self.minimum = minimum
        self.maximum = maximum
        minmax = [MinValueValidator (minimum), MaxValueValidator (maximum)]
        FloatField.__init__ (self, validators = minmax, **kwargs)

    def deconstruct(self):
        result = __, __, __, kwargs = super(FloatRangeField, self).deconstruct()
        kwargs['minimum'] = self.minimum
        kwargs['minimum'] = self.maximum
        del kwargs['validators']
        return result

请注意,最好不要使用 validators = minmax 作为参数,因为这意味着如果用户将 validator 参数用于您的 FloatRangeField 构造函数,会出现参数冲突。

Note that you better do not use validators = minmax as parameter, since that will mean that if a user would use the validator parameters for your FloatRangeField constructor, there will be a parameter clash.

例如,可以追加我们的 minmax 验证器到已经存在的验证器,然后当我们想对其进行解构时,将它们从验证器中弹出:

For example it is possible to append our minmax validators to the validators that already exist, and then later pop these back from the validator when we want to deconstruct it:

class FloatRangeField (FloatField):
    """A FloatField constrained to a given range."""

    def __init__ (self, minimum, maximum, **kwargs):
        self.minimum = minimum
        self.maximum = maximum
        old_validators = kwargs.get('validators', [])
        minmax = [MinValueValidator (minimum), MaxValueValidator (maximum)]
        kwargs['validators'] = minmax + old_validators
        FloatField.__init__ (self, **kwargs)


    def deconstruct(self):
        result = __, __, __, kwargs = super(FloatRangeField, self).deconstruct()
        kwargs['minimum'] = self.minimum
        kwargs['minimum'] = self.maximum
        kwargs['validators'] = kwargs['validators'][2:]
        return result

所以在这里,我们在 deconstruct(..)函数中,删除了前两个验证器(我们在 __ init __(..)函数中添加了)。

So here we in the deconstruct(..) function, we remove the first two validators (that we added in the __init__(..) function).

这篇关于为什么克隆FloatField的此子类(带有添加的验证器)会引发异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆