django: BooleanField,如何将默认值设置为true?

时间:2022-06-22 22:53:12

I am using BooleanField in django. By default the checkbox generated by it is unchecked state, I want the state to be checked by default, how to do it?

我在django使用BooleanField。默认情况下,由它生成的复选框是未检查的状态,我希望默认状态检查状态,如何执行?

5 个解决方案

#1


126  

If you're just using a vanilla form (not a ModelForm), you can set a Field initial value ( http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial ) like

如果您只是使用一个普通的表单(而不是一个ModelForm),您可以设置一个字段初始值(http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial)。

class MyForm(forms.Form):
    my_field = forms.BooleanField(initial=True)

If you're using a ModelForm, you can set a default value on the model field ( http://docs.djangoproject.com/en/dev/ref/models/fields/#default ), which will apply to the resulting ModelForm.

如果您使用的是ModelForm,您可以在模型字段上设置默认值(http://docs.djangoproject.com/en/dev/ref/models/fields/#default),它将应用于生成的模型。

Finally, if you want to dynamically choose at runtime whether or not your field will be selected by default, you can use the initial parameter to the form when you initialize it:

最后,如果您想在运行时动态选择您的字段是否被默认选中,您可以在初始化它时使用初始参数:

form = MyForm(initial={'my_field':True})

#2


120  

from django.db import models

class Foo(models.Model):
    any_field = models.BooleanField(default=True)

#3


4  

from django.db import models

class Foo(models.Model):
    any_field = models.BooleanField(default=True)

Solve my problem, i was needing to use in models instead of forms

解决我的问题,我需要用在模型而不是形式。

#4


1  

I am using django==1.11. The answer get the most vote is actually wrong. Checking the document from django, it says:

我使用django = = 1.11。答案是,大多数选票实际上是错误的。查看django的文档,它说:

initial -- A value to use in this Field's initial display. This value is not used as a fallback if data isn't given.

初始值——在该字段的初始显示中使用的值。如果没有提供数据,这个值不会用作回退。

And if you dig into the code of form validation process, you will find that, for each fields, form will call it's widget's value_from_datadict to get actual value, so this is the place where we can inject default value.

如果您深入到表单验证过程的代码中,您会发现,对于每个字段,表单都会调用它的value_from_datadict来获取实际值,因此这里是我们可以注入默认值的地方。

To do this for BooleanField, we can inherit from CheckboxInput, override default value_from_datadict and init function.

为了实现BooleanField,我们可以继承CheckboxInput,重写默认value_from_datadict和init函数。

class CheckboxInput(forms.CheckboxInput):
    def __init__(self, default=False, *args, **kwargs):
        super(CheckboxInput, self).__init__(*args, **kwargs)
        self.default = default

    def value_from_datadict(self, data, files, name):
        if name not in data:
            return self.default
        return super(CheckboxInput, self).value_from_datadict(data, files, name)

Then use this widget when creating BooleanField.

然后在创建BooleanField时使用此小部件。

class ExampleForm(forms.Form):
    bool_field = forms.BooleanField(widget=CheckboxInput(default=True), required=False)

#5


0  

In your models.py :

在你的模型。py:

Class a(models.model):
   Var = models.BooleanField(default=True)

Just put the desired value inside the parentheses

只需将所需的值放入括号内。

#1


126  

If you're just using a vanilla form (not a ModelForm), you can set a Field initial value ( http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial ) like

如果您只是使用一个普通的表单(而不是一个ModelForm),您可以设置一个字段初始值(http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.initial)。

class MyForm(forms.Form):
    my_field = forms.BooleanField(initial=True)

If you're using a ModelForm, you can set a default value on the model field ( http://docs.djangoproject.com/en/dev/ref/models/fields/#default ), which will apply to the resulting ModelForm.

如果您使用的是ModelForm,您可以在模型字段上设置默认值(http://docs.djangoproject.com/en/dev/ref/models/fields/#default),它将应用于生成的模型。

Finally, if you want to dynamically choose at runtime whether or not your field will be selected by default, you can use the initial parameter to the form when you initialize it:

最后,如果您想在运行时动态选择您的字段是否被默认选中,您可以在初始化它时使用初始参数:

form = MyForm(initial={'my_field':True})

#2


120  

from django.db import models

class Foo(models.Model):
    any_field = models.BooleanField(default=True)

#3


4  

from django.db import models

class Foo(models.Model):
    any_field = models.BooleanField(default=True)

Solve my problem, i was needing to use in models instead of forms

解决我的问题,我需要用在模型而不是形式。

#4


1  

I am using django==1.11. The answer get the most vote is actually wrong. Checking the document from django, it says:

我使用django = = 1.11。答案是,大多数选票实际上是错误的。查看django的文档,它说:

initial -- A value to use in this Field's initial display. This value is not used as a fallback if data isn't given.

初始值——在该字段的初始显示中使用的值。如果没有提供数据,这个值不会用作回退。

And if you dig into the code of form validation process, you will find that, for each fields, form will call it's widget's value_from_datadict to get actual value, so this is the place where we can inject default value.

如果您深入到表单验证过程的代码中,您会发现,对于每个字段,表单都会调用它的value_from_datadict来获取实际值,因此这里是我们可以注入默认值的地方。

To do this for BooleanField, we can inherit from CheckboxInput, override default value_from_datadict and init function.

为了实现BooleanField,我们可以继承CheckboxInput,重写默认value_from_datadict和init函数。

class CheckboxInput(forms.CheckboxInput):
    def __init__(self, default=False, *args, **kwargs):
        super(CheckboxInput, self).__init__(*args, **kwargs)
        self.default = default

    def value_from_datadict(self, data, files, name):
        if name not in data:
            return self.default
        return super(CheckboxInput, self).value_from_datadict(data, files, name)

Then use this widget when creating BooleanField.

然后在创建BooleanField时使用此小部件。

class ExampleForm(forms.Form):
    bool_field = forms.BooleanField(widget=CheckboxInput(default=True), required=False)

#5


0  

In your models.py :

在你的模型。py:

Class a(models.model):
   Var = models.BooleanField(default=True)

Just put the desired value inside the parentheses

只需将所需的值放入括号内。