Django中models定义的choices字典使用get_FooName_display()在页面中显示值
问题
在django的models.py 中,我们定义了一些choices的元组,类似一些字典值,一般都是下拉框或者单多选框,例如 0对应男 1对应女等等
看下例子:
class Area(models.Model):
Area_Level = (
(0, u'全国'),
(1, u'省、直辖市'),
(2, u'市、直辖市区'),
(3, u'区、县等'),
)
areaname = models.CharField(max_length=30,unique=True, verbose_name='区域名称')
code = models.CharField(max_length=20,blank=True, default="", verbose_name='区域代码')
parentid = models.IntegerField(verbose_name='父级id', null=True)
level = models.IntegerField(choices=Area_Level,verbose_name='层级', null=True)
在页面中有个table要把表中的字段显示出来,如果数据库中存储的是0就显示 全国, 1就显示省、直辖市 类似:
名称 代码 层级 上级地区 操作
全国 全国(0) 删除
北京 bj 省、直辖市(1) 全国 删除
我们可以自己定义一个函数来判断使用,但方法确实比较low,那么django中有没有这种方法可以让我们直接使用呢?
我们先来看下Django官方的解释:
Documentation for "choices" should mention "get_FOO_display"
Description
The documentation for the choices attribute on model fields mentions that choices should be a tuple of 2-tuples, including an actual value and a human-readable name, but documentation on how to retrieve the human-readable name of the selected choice from an instance of the model lives elsewhere, in the database API documentation, under the unintuitive name of get_FOO_display. This hinders a new user of Django trying to understand how best to make use of choices.
At the very least, the model documentation should include, under choices, a link to the get_FOO_display entry in the DB documentation for an explanation of how to get the human-readable name back out of a model instance. Ideally, we'd do that and come up with better titles for the sections on the various get_FOO_display/get_next_by_FOO/etc. methods.
***
在页面上我们只要这么写就可以直接把字典的值显示出来了
{{ obj.get_level_display }}({{ obj.level }})
obj.get_字段名称_display 。
models中的choices字段
由元素为2-tuples的序列(list或者tuple)作为字段的choices。2-tuple的第一个元素存储在数据库中,第二个元素可由get_FOO_display方法得到。
>>>p=Person(name='Sam',gender=1)
>>>p.save()
>>>p.gender
1
>>>p.get_gender_display()
u'Male'
Django中models定义的choices字典使用get_FooName_display()在页面中显示值的更多相关文章
- Django]models中定义的choices 字典在页面中显示值
在django的models.py 中,我们定义了一些choices的元组,类似一些字典值,一般都是下拉框或者单多选框,例如 0对应男 1对应女等 class Area(models.Model): ...
- 在django中进行后台管理时插入外键数据时不显示值的问题
在django的后台管理站点插入数据时,发现需要添加外键时,下拉框中不显示值 按照显示内容中的object,考虑这里应该是调用的模型类的objects对象方法,那么去models.py中对模型类添加一 ...
- Django把现在时间写入数据库,模板渲染在页面中
1. 导入time模块 import time 2. 获取现在时间,使用"年-月-日 时:分:秒"这样的模板,赋值给变量 在views.py中: pt = time.strftim ...
- Django学习手册 - ORM choice字段 如何在页面上显示值
在module操作过程中使用choice字段: 核心: obj.get_字段名_display 定义module 数据结构: class msg(models.Model): choice = ( ( ...
- 在jsp中用一数组存储了数据库表中某一字段的值,然后在页面中输出其中的值。
List<String> list = new ArrayList<String>(); String sql = "select userName from us ...
- struts2中的session、request 、和action往页面中传值的方法
ActionContext.getContext().put("list", list); ActionContext.getContext().getValueStack().p ...
- mvc项目中Controller执行完毕重定向到html的一个页面中
String ip = request.getLocalAddr(); //取得服务器IP int port = request.getLocalPort(); //取得服务器端口 String ur ...
- vue中使用markdown富文本,并在html页面中展示
想给自己的后台增加一个markdown编辑器,下面记录下引用的步骤 引入组件mavon-editor 官网地址:https://github.com/hinesboy/mavonEditor // 插 ...
- Matlab中函数定义方法
Matlab自定义函数的六种方法 n1.函数文件+调用函数(命令)文件:需单独定义一个自定义函数的M文件: n2.函数文件+子函数:定义一个具有多个自定义函数的M文件: n3.Inline:无需M文件 ...
随机推荐
- Delphi赋
DELPHI者,经典开发工具.美奂美仑之开发环境也. 盖论DELPHI其身世,实为神界之神物,后借宝蓝公司之手,于1990年代,现于江湖. DELPHI一出江湖,码农爱之,企业爱之.一时间,风雨雷动, ...
- centos vim 配置
centos 6.5使用源码安装vim 7.41.下载vim的源码.vim官网是www.vimunix.com/vim使用链接下载7.4源码包:ftp://ftp.vim.org/pub/vim/un ...
- Comet OJ - Contest #9 & X Round 3题解
传送门 \(A\) 咕咕 typedef long long ll; int a1,a2,n,d;ll res; int main(){ scanf("%d%d%d",&a ...
- Pytest权威教程02-Pytest 使用及调用方法
目录 Pytest 使用及调用方法 使用python -m pytest调用pytest 可能出现的执行退出code 获取版本路径.命令行选项及环境变量相关帮助 第1(N)次失败后停止测试 指定及选择 ...
- nvarchar(MAX) 、ntext的差别
今天在做一个项目时,程序成功向数据库插入一条数据, 插入的是一篇比较大的文章,而且还有很多样式. Id是int型,Content用的是 ntext类型的 但是当我在手动改数据库Id时出现错误: 错误源 ...
- P5385 [Cnoi2019]须臾幻境(LCT+主席树,思维题)
题目 P5385 [Cnoi2019]须臾幻境 做法 考虑一条边\((u,v)\)是否\([L,R]\)中的贡献:\([L,R]\)中第一条位于\(u,v\)链的边,则减少了一个联通块 实现:\(LC ...
- Java IO管道流
import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; publi ...
- IIS7 伪静态 web.config 配置方法【详解】
IIS7 做伪静态比较的简单方便 1.程序方面 只需要设置web.config 就可以了. 2.服务器需要安装:URL Rewrite 下载地址:http://www.iis.net/download ...
- 团队作业-Alpha(1/4)
队名:软工9组 组长博客: https://www.cnblogs.com/cmlei/ 作业博客: 组员进度 ● 组员一(组长) 陈明磊 ○过去两天完成了哪些任务 ●文字/口头描述 初步学习flas ...
- make 实例 二 V56
######################################################################### # # Makefile used for buil ...