在 django 表单中设置下拉菜单的默认值

发布于 2024-11-04 17:13:06 字数 536 浏览 0 评论 0原文

我无法在加载表单时设置下拉菜单的默认值。

的代码

state = forms.TypedChoiceField(choices = formfields.State)

State = (
         ('QC_APPROVED','QC_APPROVED'),
         ('REVERT','REVERT'),
         ('FIXED','FIXED'),
        )

这是如果我想将默认状态设置为“固定” 。我正在编写这段代码

state = forms.TypedChoiceField(choices = formfields.State, default = 'FIXED')

如果我执行上面的代码,我会收到以下错误。

Exception Value: __init__() got an unexpected keyword argument 'default'

有人可以帮忙解决这个问题吗?

I am unable to set default value for a dropdown while loading forms.

Here is the code

state = forms.TypedChoiceField(choices = formfields.State)

State = (
         ('QC_APPROVED','QC_APPROVED'),
         ('REVERT','REVERT'),
         ('FIXED','FIXED'),
        )

If I want to make the default state as FIXED. I am writing this code

state = forms.TypedChoiceField(choices = formfields.State, default = 'FIXED')

If I execute the above code I am getting the below error.

Exception Value: __init__() got an unexpected keyword argument 'default'

Can some one help on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一萌ing 2024-11-11 17:13:06
state = forms.TypedChoiceField(choices=formfields.State, initial='FIXED')

如文档所示: http://docs.djangoproject.com/en /dev/ref/forms/fields/#initial

state = forms.TypedChoiceField(choices=formfields.State, initial='FIXED')

As shown in documentation: http://docs.djangoproject.com/en/dev/ref/forms/fields/#initial

浊酒尽余欢 2024-11-11 17:13:06

我在寻找如何为外键字段设置 Django 表单的初始“选定”状态时遇到了这个线程,所以我只想补充一下,您可以按如下方式执行此操作:

models.py:

class Thread(NamedModel):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    title = models.CharField(max_length=70, blank=False)

forms.py:

class ThreadForm(forms.ModelForm):
    class Meta:
        model = Thread
        fields = ['topic', 'title']

views.py:

def createThread(request, topic_title):
    topic = Topic.getTopic(topic_title)
    threadForm = ThreadForm(initial={'topic': topic.id})
...

关键是设置 initial={'topic': topic.id} ,但没有详细记录在我看来。

I came across this thread while looking for how to set the initial "selected" state of a Django form for a foreign key field, so I just wanted to add that you do this as follows:

models.py:

class Thread(NamedModel):
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    title = models.CharField(max_length=70, blank=False)

forms.py:

class ThreadForm(forms.ModelForm):
    class Meta:
        model = Thread
        fields = ['topic', 'title']

views.py:

def createThread(request, topic_title):
    topic = Topic.getTopic(topic_title)
    threadForm = ThreadForm(initial={'topic': topic.id})
...

The key is setting initial={'topic': topic.id} which is not well documented in my opinion.

素食主义者 2024-11-11 17:13:06

字段采用 initial

fields take initial values

沧桑㈠ 2024-11-11 17:13:06
state = forms.TypedChoiceField(choices=formfields.State, initial='FIXED')

title = forms.CharField(widget=forms.Select(choices=formfields.State) , initial='FIXED')

toppings = forms.ChoiceField(
        widget=forms.Select(attrs={'class':"hhhhhhhh"}),
        choices = formfields.State,
        initial='FIXED'
    )
state = forms.TypedChoiceField(choices=formfields.State, initial='FIXED')

title = forms.CharField(widget=forms.Select(choices=formfields.State) , initial='FIXED')

toppings = forms.ChoiceField(
        widget=forms.Select(attrs={'class':"hhhhhhhh"}),
        choices = formfields.State,
        initial='FIXED'
    )
那小子欠揍 2024-11-11 17:13:06

如果其他解决方案不适合你,试试这个:

原来ModelChoiceField有一个名为empty_label的属性。使用空_label你可以输入一个默认值供用户查看。
示例:在 forms.py 中

Class CreateForm(forms.ModelForm):
      category = forms.ModelChoiceField(empty_label="Choose Category")

If the other solutions dont work for you,Try this:

It turns out that ModelChoiceField has an attribute called empty_label.With empty _label you can enter a default value for the user to see.
Example: In forms.py

Class CreateForm(forms.ModelForm):
      category = forms.ModelChoiceField(empty_label="Choose Category")
情释 2024-11-11 17:13:06

试试号码:

state = forms.TypedChoiceField(choices = formfields.State, default = 2 )

Try the number:

state = forms.TypedChoiceField(choices = formfields.State, default = 2 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文