本文介绍了选择字段中的Django空标签 - 无查询集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在选择字段上设置空标签会给我一些问题。我已经查看了诸如的答案,但这只是谈论表单。

  class MyColorModel(models.Model ):
BLUE ='蓝色'
RED ='红色'
绿色='绿色'
COLOR_CHOICES =(
(蓝色,'蓝色'),
(RED,'Red'),
(GREEN,'Green'))

my_color = models.CharField(max_length = 5,choices = COLOR_CHOICES,blank = True)

这种形式:

  class MyColorForm(forms.ModelForm):$ b $ class meta:
model = MyColorModel
fields = ['mycolor']

默认情况下,这将显示一个选择字段,空白标签设置为------



我想出了以下选项来设置标签。



1)Chan我的模特中的COL COL_CHOICES转换为:

  COLOR_CHOICES =(
('','' ---请选择您的颜色---'),
(蓝色,'蓝色'),
(红色,'红色'),
(绿色,'绿色'),

2)将模型中的COLOR_CHOICES更改为:

  COLOR_CHOICES =(
(None,'---请选择您的颜色---'),
(BLUE,'蓝色'),
(红色,'红色'),
(绿色,'绿色'),

3)将以下 init 方法添加到我的模型表单

  def __init __(* args,** kwargs):
super(MyColorForm,self).__ init __(* args,** kwargs)
self.fields ['my_color'] .choices = [('','---请选择您的颜色---')] + MyColorModel.COLOR_CHOICES

选项1导致一些副作用,如果我已经开始失败的话,那么这是什么导致我创建选项2.



是否可以将空白标签添加到我的模型中(选项2)?任何副作用,如果我添加它,或者它是更好的添加它的形式?它只是看起来像很多代码,只是当我在表单中设置空标签时。

设置什么你想在表单上做一个更正确的方法来实现你想要的。在Django中,模型包含要存储的数据的基本字段和行为。也就是说,他们定义了您的信息在数据库中的结构。



1.我没有检查它,但是您的第一个选项是

 COLOR_CHOICES =(
('','---请选择您的颜色---'),
(蓝色,'蓝色'),
(RED,'Red'),
(GREEN,'Green'),

my_color = models.CharField(max_length = 5,choices = COLOR_CHOICES)
my_color $ b

可能不起作用,字段不包含 blank = True 参数,这意味着它不允许将空字符串保存到数据库中,但是如果你希望这个字段是强制的,那么您的空选择的定义不应该在这里,因为它没有带来任何附加价值,因为它不包含任何关于您的数据结构的额外信息。



2.您的第二个选择是绝对不是要走的路:



你的第三个选择看起来不错。我有一段时间没有用django表单工作,但是如果你的代码有效,那么Form就是一个更合适的地方来定义你的表单应该看起来像在模型中做的样子,所以去那个选项。

或者看一下您帖子中链接的SO问题的解决方案并实现它们。



祝您好运! p>

Setting the empty label for on a choice field is giving me some issues. I've looked at answers like this, but this is just talking about the form.

Suppose I have the following model:

class MyColorModel(models.Model):
    BLUE = 'blue'
    RED = 'red'
    GREEN = 'green'
    COLOR_CHOICES = (
        (BLUE, 'Blue'),
        (RED, 'Red'),
        (GREEN, 'Green'))

   my_color = models.CharField(max_length=5, choices=COLOR_CHOICES, blank=True)

And this form:

class MyColorForm(forms.ModelForm):
   class Meta:
       model = MyColorModel
       fields = ['mycolor']

By default, this will display a choice field, with the blank label set to ------

I've came up with the following options to set the label.

1) Change COLOR_CHOICES in my model to:

COLOR_CHOICES = (
    ('', '---Please select your color---'),
    (BLUE, 'Blue'),
    (RED, 'Red'),
    (GREEN, 'Green'),

2) Change COLOR_CHOICES in my model to:

COLOR_CHOICES = (
    (None, '---Please select your color---'),
    (BLUE, 'Blue'),
    (RED, 'Red'),
    (GREEN, 'Green'),

3) Add the following init method to my model form:

def __init__(*args, **kwargs):
    super(MyColorForm, self).__init__(*args, **kwargs)
    self.fields['my_color'].choices =  [('', '---Please select your color---')] + MyColorModel.COLOR_CHOICES

Options 1 led to some side effects where if statements that I had started failing, that's what led me to create option 2.

Is it fine adding the blank label to my model (option 2)? Any side effects if I add it there, or is it better to add it on the form? It just seems like quite a lot of code just to set the empty label when I do it in the form.

解决方案

Setting what you want on the form is a more correct way to do achieve what you want. In Django, models contain the essential fields and behaviors of the data you’re storing. That said, they define how your information is structured in the database.

1. I didn't check it, but your first option

COLOR_CHOICES = (
    ('', '---Please select your color---'),
    (BLUE, 'Blue'),
    (RED, 'Red'),
    (GREEN, 'Green'),

my_color = models.CharField(max_length=5, choices=COLOR_CHOICES)

might not work, as your first choice is an empty string, but your my_color field does not contain the blank=True argument. This means it won't allow saving the empty string to the database. But in case you want this field to be mandatory, then your definition of the empty choice should not be here, as it does not bring any added value as it does not hold any additional information regarding your data structure.

2. Your second option is definitely not the way to go [1]:

3. Your third option looks good. I didn't work for some time with django forms, but if your code works, the Form is a more suitable place to define how your form should look like than doing it in your models, so go for that option.

Or take a look at the solutions from the SO question linked in your post and implement those.

Good luck!

这篇关于选择字段中的Django空标签 - 无查询集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 12:16