django:如何在模型形式中更改nullbooleanfield的值? [英] django: how to change values for nullbooleanfield in a modelform?

查看:153
本文介绍了django:如何在模型形式中更改nullbooleanfield的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档中, nullbooleanfield 表示为作为< select> 如何将 select 的值更改为其他一些更有意义的文本,以及将其映射回我的 modelform 中的yes,no和未知值?

In the docs, the nullbooleanfield is represented as a <select> box with "Unknown", "Yes" and "No" choices. How can I change the values of select to some other more meaningful texts and map it back to the yes,no and unknown values in my modelform?

例如我有 yes_no_required = models.NullBooleanField(),我想有'是的,我承认这一点''否,我不喜欢这样''我现在不知道'映射为是,否,并且相应地需要。

For example I have yes_no_required = models.NullBooleanField() and I would like to have 'yes I acknowledge this', 'no, I do not like this' and 'I do not know now' mapping to yes, no and required accordingly.

推荐答案

我花了半个小时整理了一个演示,目的是向我自己证明这个方法:

I spent half an hour putting together a demo just to prove to myself this works:

CHOICES = (
    (None, "I do not know now"),
    (True, "Yes I acknowledge this"),
    (False, "No, I do not like this")
)

class Demo(models.Model):
    yes_no_required = models.NullBooleanField(choices = CHOICES)

class DemoForm(forms.ModelForm):
    class Meta:
        model = Demo

我查看了Django源代码。 一切都可以接受选择数组。显然,对于其中一些没有道理的事情,但这是有可能的。

I looked in the Django source code. Everything can accept a choices array. Obviously, for some of those that doesn't make sense, but it's possible.

这篇关于django:如何在模型形式中更改nullbooleanfield的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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