Django:列表中没有的下拉列表值

时间:2023-01-07 13:14:28

I'm trying to input a value into drop-down list that is NOT on the list using Django. A default value is read from the database.
But the trick is, that the user can see the value, but not change to it.

我正在尝试使用Django将值输入到不在列表中的下拉列表中。从数据库中读取默认值。但诀窍是,用户可以看到值,但不能改变它。

Is there a way to do it with Django?
Django won't allow me to display in drop-down list something that is not defined in models.py, so the object remains empty.

有没有办法用Django做到这一点? Django不允许我在下拉列表中显示一些未在models.py中定义的内容,因此该对象保持为空。

Any ideas? Maybe some walk-around?

有任何想法吗?也许一些走动?

Thanks

Code:

aList = (('OK', 'OK'), ('Not', 'Not'))

class sampleModel(models.Model):
  name = models.CharField(max_length=32, blank=True)
  res = models.CharField(max_length=10, blank=True, choices=aList)

From another database comes entry OK* and Not*. I need to display that OK*/Not* in drop-down list but so that user cannot use it, but still can change to normal OK/Not.

从另一个数据库进入OK *和Not *。我需要在下拉列表中显示OK * / Not *,但是用户不能使用它,但仍然可以更改为正常OK / Not。

1 个解决方案

#1


1  

One simple option could be to add an empty choice to your choices like:

一个简单的选择可能是为您的选择添加一个空选择,如:

aList = (
    ('', 'OK/Not') 
    ('OK', 'OK'), 
    ('Not', 'Not')
)

and make the field res to be required, so that you cannot pass an empty value, so if the user selects the empty choice, the form will return error until he choose one of the valid choices

并使字段res成为必需,以便您不能传递空值,因此如果用户选择空选项,表单将返回错误,直到他选择其中一个有效选项

You should change your field:

你应该改变你的领域:

res = models.CharField(max_length=10, blank=True, choices=aList)

for:

res = models.CharField(max_length=10, choices=aList)

The only change made was to remove blank=True so the field is requried.

唯一的变化是删除blank = True,因此需要该字段。

All of this assuming you have some Django ModelForm that you generated based on your model

所有这一切都假设您有一些基于您的模型生成的Django ModelForm

Other option to avoid modifying the model could be to manage the select in the view. If you have the select in the HTML with 3 choiceS:

避免修改模型的其他选项可能是管理视图中的选择。如果您在HTML中选择了3个选项:

<select name="mySelect" id="id_mySelect">
  <option value="" selected="selected">Please select value</option>
  <option value="value1">Ok</option>
  <option value="value2">Not</option>
</select>

So when you receive the POST in your view, you could do something like:

因此,当您在视图中收到POST时,您可以执行以下操作:

error = False
error_text = ''
# Manually get the value of the select in your view
myselect = request.POST.get('mySelect', None)

# If the value of the select is '' return an error to the template
if not myselect:
   error = True
   error_text = 'You have to select an option'
   return ('yourtemplate.html', {'error':error, 'error_text': error_text}, context_instance=RequestContext(request) )

and you only need to manage that custom error you're returning to the template

并且您只需要管理您返回到模板的自定义错误

You really have a lot possibilities to do what you're trying to do, but to help us helping you we need more information about your project, usually some parts of the code you have right now and how are you managing it too.

你真的有很多可能做你正在做的事情,但为了帮助我们,我们需要更多关于你的项目的信息,通常是你现在拥有的代码的一部分,以及你如何管理它。

Options to do what you want:

做你想做的事情的选项:

  • Make the field required and use ModelForm
  • 使字段成为必需字段并使用ModelForm

  • Make a custom form and customize the field validation (as @dukeboy suggested)
  • 制作自定义表单并自定义字段验证(如@dukeboy建议的那样)

  • Make the select pure HTML and manage it on the view
  • 选择纯HTML并在视图上进行管理

  • ...

#1


1  

One simple option could be to add an empty choice to your choices like:

一个简单的选择可能是为您的选择添加一个空选择,如:

aList = (
    ('', 'OK/Not') 
    ('OK', 'OK'), 
    ('Not', 'Not')
)

and make the field res to be required, so that you cannot pass an empty value, so if the user selects the empty choice, the form will return error until he choose one of the valid choices

并使字段res成为必需,以便您不能传递空值,因此如果用户选择空选项,表单将返回错误,直到他选择其中一个有效选项

You should change your field:

你应该改变你的领域:

res = models.CharField(max_length=10, blank=True, choices=aList)

for:

res = models.CharField(max_length=10, choices=aList)

The only change made was to remove blank=True so the field is requried.

唯一的变化是删除blank = True,因此需要该字段。

All of this assuming you have some Django ModelForm that you generated based on your model

所有这一切都假设您有一些基于您的模型生成的Django ModelForm

Other option to avoid modifying the model could be to manage the select in the view. If you have the select in the HTML with 3 choiceS:

避免修改模型的其他选项可能是管理视图中的选择。如果您在HTML中选择了3个选项:

<select name="mySelect" id="id_mySelect">
  <option value="" selected="selected">Please select value</option>
  <option value="value1">Ok</option>
  <option value="value2">Not</option>
</select>

So when you receive the POST in your view, you could do something like:

因此,当您在视图中收到POST时,您可以执行以下操作:

error = False
error_text = ''
# Manually get the value of the select in your view
myselect = request.POST.get('mySelect', None)

# If the value of the select is '' return an error to the template
if not myselect:
   error = True
   error_text = 'You have to select an option'
   return ('yourtemplate.html', {'error':error, 'error_text': error_text}, context_instance=RequestContext(request) )

and you only need to manage that custom error you're returning to the template

并且您只需要管理您返回到模板的自定义错误

You really have a lot possibilities to do what you're trying to do, but to help us helping you we need more information about your project, usually some parts of the code you have right now and how are you managing it too.

你真的有很多可能做你正在做的事情,但为了帮助我们,我们需要更多关于你的项目的信息,通常是你现在拥有的代码的一部分,以及你如何管理它。

Options to do what you want:

做你想做的事情的选项:

  • Make the field required and use ModelForm
  • 使字段成为必需字段并使用ModelForm

  • Make a custom form and customize the field validation (as @dukeboy suggested)
  • 制作自定义表单并自定义字段验证(如@dukeboy建议的那样)

  • Make the select pure HTML and manage it on the view
  • 选择纯HTML并在视图上进行管理

  • ...