自定义/删除Django选择框空白选项 [英] Customize/remove Django select box blank option

查看:159
本文介绍了自定义/删除Django选择框空白选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.0.2。我编写了一个ModelForm,由Model支持。这个模型有一个ForeignKey,其中blank = False。当Django为此表单生成HTML时,它将为ForeignKey引用的表中的每一行创建一个选项框。它还创建了一个列表顶部的选项,没有值,并显示为一系列破折号:

 < option值=  > ---------< /选项> 

我想知道的是:


  1. 从选择框中删除此自动生成的选项最干净的方法是什么?

  2. 什么是最干净的自定义方式,以便显示为:

     < option value =>选择项< / option> 


在搜索解决方案时,一个href =http://code.djangoproject.com/ticket/4653 =noreferrer> Django ticket 4653 ,这给我的印象是别人有同样的问题,Django的默认行为可能已被修改。这张票是一年多以前,所以我希望有一个更清洁的方式来完成这些事情。



感谢任何帮助,



Jeff



编辑:我已经配置了ForeignKey字段:

  verb = models.ForeignKey(Verb,blank = False,default = get_default_verb)

这将设置默认值,使其不再是空的/破折号的选项,但不幸的是它似乎不能解决我的任何一个问题。也就是说,空/破折号选项仍然出现在列表中。

解决方案

没有测试过,但是基于阅读Django的代码此处这里我相信它应该工作:

  class ThingForm(models.ModelForm):
class Meta:
model = Thing

def __init __(self ,* args,** kwargs):
super(ThingForm,self).__ init __(* args,** kwargs)
self.fields ['verb']。empty_label = None

编辑:这是记录的文件,虽然如果您使用自动生成的ModelForm,您不一定知道要查找ModelChoiceField。 p >

编辑:正如jlpp在他的答案中所说明的那样,这还不完整 - 在更改empty_label属性后,您必须重新将选项分配给小部件。由于这有点恶作剧,另一个可能更容易理解的选项就是覆盖整个ModelChoiceField:

  class ThingForm(models .ModelForm):
verb = ModelChoiceField(Verb.objects.all(),empty_label = None)

class Meta:
model = Thing


I'm using Django 1.0.2. I've written a ModelForm backed by a Model. This model has a ForeignKey where blank=False. When Django generates HTML for this form it creates a select box with one option for each row in the table referenced by the ForeignKey. It also creates an option at the top of the list that has no value and displays as a series of dashes:

<option value="">---------</option>

What I'd like to know is:

  1. What is the cleanest way to remove this auto-generated option from the select box?
  2. What is the cleanest way to customize it so that it shows as:

    <option value="">Select Item</option>
    

In searching for a solution I came across Django ticket 4653 which gave me the impression that others had the same question and that the default behavior of Django may have been modified. This ticket is over a year old so I was hoping there might be a cleaner way to accomplish these things.

Thanks for any help,

Jeff

Edit: I've configured the ForeignKey field as such:

verb = models.ForeignKey(Verb, blank=False, default=get_default_verb)

This does set the default so that it's no longer the empty/dashes option but unfortunately it doesn't seem to resolve either of my questions. That is, the empty/dashes option still appears in the list.

解决方案

Haven't tested this, but based on reading Django's code here and here I believe it should work:

class ThingForm(models.ModelForm):
  class Meta:
    model = Thing

  def __init__(self, *args, **kwargs):
    super(ThingForm, self).__init__(*args, **kwargs)
    self.fields['verb'].empty_label = None

EDIT: This is documented, though you wouldn't necessarily know to look for ModelChoiceField if you're working with an auto-generated ModelForm.

EDIT: As jlpp notes in his answer, this isn't complete - you have to re-assign the choices to the widgets after changing the empty_label attribute. Since that's a bit hacky, the other option that might be easier to understand is just overriding the entire ModelChoiceField:

class ThingForm(models.ModelForm):
  verb = ModelChoiceField(Verb.objects.all(), empty_label=None)

  class Meta:
    model = Thing

这篇关于自定义/删除Django选择框空白选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆