I am working with Django and its Forms classes.
我正在使用Django及其Forms类。
I have created a Form with a single forms.IntegerField
and I want to define its min_value
parameter dynamically:
我已经创建了一个具有单个表单的表单。IntegerField和我想要动态定义它的min_value参数:
class BiddingForm(forms.Form):
bidding_form = forms.IntegerField(min_value=1, help_text="€", label="Bid")
Right now, it is statically set to 1
. I have tried to overwrite the __init__()
function and pass in a min_value
there. But since bidding_form
is a class variable, I cannot use the resulting instance variable min_value
on it:
现在,它被静态地设置为1。我试图覆盖__init__()函数,并在那里传递一个min_value。但是由于bidding_form是一个类变量,我不能在它上面使用生成的实例变量min_value:
class BiddingForm(forms.Form):
min_value = 1
def __init__(self, min_value):
super().__init__()
self.min_value = min_value
bidding_form = forms.IntegerField(min_value=min_value, help_text="€", label="Bid")
As of my understanding, above class creates an instance variable min_value
inside of __init__()
which just shadows the class variable min_value
and it ultimately results in min_value
being 1
in the bidding_form
's declaration.
Since I have little understanding of Python and Django, I have not yet found a solution to this.
So, how can I dynamically define forms.IntegerField
's min_value
parameter?
据我所知,上面的类在__init__()中创建了一个实例变量min_value,它只是隐藏了类变量min_vvalue,最终导致在bidding_form的声明中min_value为1。由于我对Python和Django了解不多,所以我还没有找到解决方案。那么,我如何动态定义表单呢。IntegerField的min_value参数?
更多回答
优秀答案推荐
You override the min_value
of the field:
覆盖字段的最小值(_V):
from django.core import validators MinValueValidator
class BiddingForm(forms.Form):
bidding_form = forms.IntegerField(help_text='€', label='Bid')
def __init__(self, *args, min_value=None, **kwargs):
super().__init__(*args, **kwargs)
bidding_form = self.fields['bidding_form']
bidding_form.min_value = min_value
if min_value is not None:
bidding_form.validators.append(MinValueValidator(min_value))
bidding_form.widget.attrs['min'] = min_value
Correct way is:
正确的方法是:
class BidForm(forms.Form):
bid = forms.IntegerField()
def __init__(self, min_bid_value=0, *args, **kwargs):
super(BidForm, self).__init__(*args, **kwargs)
self.fields['bid'].widget.attrs['min'] = min_bid_value
You can call it like :
你可以这样称呼它:
BidForm(min_bid_value=145)
and form won't let user enter number less than 145.
表单不允许用户输入小于145的数字。
i dont think so but you can do check the length of the specified field before you check if form.is_valid():
. like: if len(form.cleaned_data['field']) < min:
我不这么认为,但你可以先检查指定字段的长度,然后再检查form.is_valid():.like:if len(form.cleane_data['field'])
更多回答
Thank you for the fast answer. Unfortunately, I have encoutered 2 problems. When I pass self
to super().__init()
I am getting 'BiddingForm' object has no attribute 'get'
. When I leave it, the site is shown but the min_value
is ignored and my IntegerField
can now have any value. How is that?
谢谢你的快速回答。不幸的是,我遇到了两个问题。当我把self传递给super()时__init()我正在获取“BiddingForm”对象没有属性“get”。当我离开它时,会显示站点,但min_value被忽略,我的IntegerField现在可以有任何值。怎么了?
我是一名优秀的程序员,十分优秀!