问题

在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()在页面中显示值的更多相关文章

  1. Django]models中定义的choices 字典在页面中显示值

    在django的models.py 中,我们定义了一些choices的元组,类似一些字典值,一般都是下拉框或者单多选框,例如 0对应男 1对应女等 class Area(models.Model): ...

  2. 在django中进行后台管理时插入外键数据时不显示值的问题

    在django的后台管理站点插入数据时,发现需要添加外键时,下拉框中不显示值 按照显示内容中的object,考虑这里应该是调用的模型类的objects对象方法,那么去models.py中对模型类添加一 ...

  3. Django把现在时间写入数据库,模板渲染在页面中

    1. 导入time模块 import time 2. 获取现在时间,使用"年-月-日 时:分:秒"这样的模板,赋值给变量 在views.py中: pt = time.strftim ...

  4. Django学习手册 - ORM choice字段 如何在页面上显示值

    在module操作过程中使用choice字段: 核心: obj.get_字段名_display 定义module 数据结构: class msg(models.Model): choice = ( ( ...

  5. 在jsp中用一数组存储了数据库表中某一字段的值,然后在页面中输出其中的值。

    List<String> list = new ArrayList<String>();  String sql = "select userName from us ...

  6. struts2中的session、request 、和action往页面中传值的方法

    ActionContext.getContext().put("list", list); ActionContext.getContext().getValueStack().p ...

  7. mvc项目中Controller执行完毕重定向到html的一个页面中

    String ip = request.getLocalAddr(); //取得服务器IP int port = request.getLocalPort(); //取得服务器端口 String ur ...

  8. vue中使用markdown富文本,并在html页面中展示

    想给自己的后台增加一个markdown编辑器,下面记录下引用的步骤 引入组件mavon-editor 官网地址:https://github.com/hinesboy/mavonEditor // 插 ...

  9. Matlab中函数定义方法

    Matlab自定义函数的六种方法 n1.函数文件+调用函数(命令)文件:需单独定义一个自定义函数的M文件: n2.函数文件+子函数:定义一个具有多个自定义函数的M文件: n3.Inline:无需M文件 ...

随机推荐

  1. URLSearchParams对象

    URLSearchParams对象用于处理URL中查询字符串,即?之后的部分. 1.语法 其实例对象的用法和Set数据结构类似.实例对象本身是可遍历对象.但是不是遍历器. var paramsStri ...

  2. Oracle trunc() 日期、数字截取函数

    --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013- ...

  3. (尚014)Vue过渡与动画

    操作元素时有个过渡或动画的效果(渐变和移动的效果和放大缩小的效果) 过渡:trasition 动画:animation 1.vue动画的理解 1)操作css的trasition或animation(它 ...

  4. 一个简单的直播demo for java

    obs推流,nginx挂rtmp模块,配置rtmp端口,obs向此端口推流,video.js H5拉流播放 加阿里CDN 超级简单- -

  5. java实现大文件上传分片上传断点续传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  6. 2019.11.15 JQ图片轮播

    <div class="three"> <div class="bjtp"> <img class="bjpic b1& ...

  7. Qt文件读写操作

    原文地址:https://www.cnblogs.com/flowingwind/p/8336159.html QFile Class 1.read读文件 加载文件对象  QFile file(&qu ...

  8. 交互设计算法基础(1) - Binary Search

    int binary_search(int[] list, int item) { int low = 0; int high = list.length-1; while (low <= hi ...

  9. secureCRT连接服务器和文件传输( 一步搞定)

    1.在百度云盘存有此工具,获取到后解压执行即可,如下2 连接目标服务器 192.xxx.xx.xx 2.secureCRT连接服务器和文件传输 ,现象如下 登录后切换到root用户即可有权限操作    ...

  10. 面试题 int(3) int(10) 区别

    1.MySQL 中 int(3) int(10) 区别 答案 存储大小并无差异,只是不足位数的时候,左边补0. ###补充知识点 创建数据库表时,加zerofill ,可以看出效果.mysql 默认 ...