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文件 ...
随机推荐
- 自定义异步IO框架
异步就是回调 异步 = 非阻塞+循环 select只能完成IO多路复用,不能完成异步 IO多路复用--->监听多个socket对象,这个过程是同步的 利用其特性可以开发异步模块 异步IO:非阻塞 ...
- go内置的反向代理
package main import ( "log" "net/http" "net/http/httputil" "net/u ...
- 双向认证下rpc-gateway使用(同时提供rpc和http服务)
下载go get -v github.com/grpc-ecosystem/grpc-gateway,然后把这个包下面的third-party下面的google文件夹拷贝到Prod.proto的同级目 ...
- ssh远程后台运行
ssh hadoop8 "/export/server/storm/bin/storm nimbus >/export/server/storm/nimbus_start.log 2& ...
- noi.ac #44 链表+树状数组+思维
\(des\) 给出长度为 \(n\) 的序列,全局变量 \(t\),\(m\) 次询问,询问区间 \([l, r]\) 内出现次数为 \(t\) 的数的个数 \(sol\) 弱化问题:求区间 \([ ...
- 42、JDBC数据源案例
一.JDBC数据源案例 1.概述 Spark SQL支持使用JDBC从关系型数据库(比如MySQL)中读取数据.读取的数据,依然由DataFrame表示,可以很方便地使用Spark Core提供的各种 ...
- webstorm 和 vscode 的纠结
自从知道了 vscode 之后,尝试了一下后,就开始纠结是否要更换到vscode. 特别是在 webstorm 卡的时候. 也研究了几日 vscode,安装了许多插件. 我是安装插件后, 就开始学习快 ...
- JSP简单标签的开发
1. 新建RepeatSampleTag类,代码如下: package bid.zhazhapan.fims.tag; import java.io.IOException; import java. ...
- UOJ#401. 【CTSC2018】青蕈领主 分治,FFT
原文链接www.cnblogs.com/zhouzhendong/p/UOJ401.html 题解 首先,对于一个排列,它的连续段一定只有包含关系,没有相交关系. 我们可以据此得到一棵表示连续段的树. ...
- LeetCode之打家劫舍
1. 问题 在一条直线上,有n个房屋,每个房屋中有数量不等的财宝,有一个盗 贼希望从房屋中盗取财宝,由于房屋中有报警器,如果同时从相邻的两个房屋中盗取财宝就会触发报警器.问在不触发报警器的前提下,最多 ...