gpt4 book ai didi

python - 从 RadioSelect 中删除空白 "---------"

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:04 24 4
gpt4 key购买 nike

我找不到从显示为 --------Choices_list 中删除第一行的方法。

我已经尝试了一些东西:blank=True, Blank=False, ...这一行仍然存在。

这是我在我的 Django 网站上得到的:

enter image description here

我的 models.pyforms.py 文件看起来像:

# models.py
# coding: utf-8

from django.db import models
from django.utils.encoding import force_text

FAVORITE_THEME = (
('Datasystems', 'Datasystems'),
('Cameroun', 'Cameroun'),
)

class Theme(models.Model):
favorite_theme = models.CharField(max_length = 20, choices=FAVORITE_THEME, verbose_name="Sélectionner le thème")



# forms.py
#-*- coding: utf-8 -*-

from django import forms
from django.forms import ModelForm
from .models import Theme

class ThemeForm(forms.ModelForm):
class Meta:
model = Theme
widgets = {'favorite_theme' : forms.RadioSelect,}
fields=('favorite_theme',)

您是否有删除或隐藏此行的想法?

谢谢

最佳答案

来自 Django ModelForm docs :

If the model field has choices set, then the form field’s widget will be set to Select, with choices coming from the model field’s choices. The choices will normally include the blank choice which is selected by default. If the field is required, this forces the user to make a selection. The blank choice will not be included if the model field has blank=False and an explicit default value (the default value will be initially selected instead).

因此,要排除表单中的空白选择,必须同时提供 blank=Falsedefault。仅设置 blank=False 将不起作用。

关于python - 从 RadioSelect 中删除空白 "---------",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42390311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com