问题

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

看下例子:

  1. class Area(models.Model):
  2. Area_Level = (
  3. (0, u'全国'),
  4. (1, u'省、直辖市'),
  5. (2, u'市、直辖市区'),
  6. (3, u'区、县等'),
  7. )
  8. areaname = models.CharField(max_length=30,unique=True, verbose_name='区域名称')
  9. code = models.CharField(max_length=20,blank=True, default="", verbose_name='区域代码')
  10. parentid = models.IntegerField(verbose_name='父级id', null=True)
  11. level = models.IntegerField(choices=Area_Level,verbose_name='层级', null=True)

在页面中有个table要把表中的字段显示出来,如果数据库中存储的是0就显示 全国, 1就显示省、直辖市 类似:

  1. 名称 代码 层级 上级地区 操作
  2. 全国 全国(0) 删除
  3. 北京 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.

***

在页面上我们只要这么写就可以直接把字典的值显示出来了

  1. {{ obj.get_level_display }}({{ obj.level }})

obj.get_字段名称_display 。

models中的choices字段

由元素为2-tuples的序列(list或者tuple)作为字段的choices。2-tuple的第一个元素存储在数据库中,第二个元素可由get_FOO_display方法得到。

  1. >>>p=Person(name='Sam',gender=1)
  2. >>>p.save()
  3. >>>p.gender
  4. 1
  5. >>>p.get_gender_display()
  6. 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. 使用LoadRunner脚本并发下载文件,出现19890错误

    需求:10个客户并发下载同一份zip文件.执行的时候,8个Fail了,只下载了两份zip,且无论执行多少遍,都是这样. 错误信息如下:message code:-19890C interpreter ...

  2. Tensorflow细节-P199-数据集

    数据集的基本使用方法 import tempfile import tensorflow as tf input_data = [1, 2, 3, 5, 8] # 这不是列表吗,为什么书里叫数组 da ...

  3. 批量把pod实例调成0

    #!/bin/bash namespace=`kubectl get ns|grep '^te-'|grep -v datamgr|awk '{print $1}'` #--------------- ...

  4. dp--01背包,完全背包,多重背包

    背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...

  5. AJAX里使用模板引擎

    一.概述: 处理响应数据渲染可以使用模板引擎(实际上就是一个API,目的是更容易的将数据渲染到HTML中) 目前市面上有许多模板引擎,可以参考 推荐使用artTemplate,它采用作用域预声明的技术 ...

  6. PHP-FPM参数详情

    pid = run/php-fpm.pid #pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启 error_log = log/php-fpm.log #错误日志,默认在 ...

  7. 基于verilog的分频器设计(奇偶分频原理及其电路实现:上)

    在一个数字系统中往往需要多种频率的时钟脉冲作为驱动源,这样就需要对FPGA的系统时钟(频率太高)进行分频.分频器主要分为奇数分频,偶数分频,半整数分频和小数分频,在对时钟要求不是很严格的FPGA系统中 ...

  8. Strategic game(树形DP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:一棵树,要放置哨兵,要求最少放置多少哨兵能监视到所有的结点 题目分析: 放置哨兵无非两 ...

  9. mac webstorm 安装破解

    下载: 链接:https://pan.baidu.com/s/1A1afhcpPWMrQtOr1Suqs-g  密码:5r7b 激活码 K6IXATEF43-eyJsaWNlbnNlSWQiOiJLN ...

  10. bootstrap select 多选的用法,取值和赋值(取消默认选择第一个的对勾)

    h5自带的select标签可以实现按住ctrl键多选的功能,但是样式及其难看. bootstrap select是很好用的前端插件 ​ 首先引入bootstrap和bootstrap-select的c ...