问题
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")
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")
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'
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'
Why is this breaking and how should I implement FloatRangeField
so as to encapsulate the having of min/max validators?
回答1:
Like is written in the documentation on custom model fields:
If you add a new keyword argument, you need to write code to put its value into
kwargs
yourself (...)
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
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.
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
So here we in the deconstruct(..)
function, we remove the first two validators (that we added in the __init__(..)
function).
来源:https://stackoverflow.com/questions/50951852/why-does-cloning-this-subclass-of-floatfield-with-added-validators-throw-an-ex