问题

在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. HDFS的NameNode中的Namespace管理

    在NameNode中的Namespace管理层是负责管理整个HDFS集群文件系统的目录树以及文件与数据块的映射关系.以下就是Namespace的内存结构: 以上是一棵文件目录树,可见Namespace ...

  2. gdb, pdb笔记

    gdb gdb --args yourprogram 常用命令 r(run):从头开始运行 c(continue):继续运行 b(breakpoint) filepath:line or namesp ...

  3. [Kubernetes] Defining a Pod with YAML

    1. Define a yml file: nginx.pod.yml: apiVersion: v1 kind: Pod metadata: name: my-nginx labels: app: ...

  4. Kibana<6.6.0代码执行漏洞复现

    更多内容,欢迎关注微信公众号:信Yang安全,期待与您相遇. 使用docker快速部署环境docker pull kibana:6.5.4docker pull elasticsearch:6.5.4 ...

  5. learning armbian steps(1) ----- armbian 入门知识基础学习

    第一问: armbian是什么? Armbian是轻量级的Debian系统和为ARM开发板专门发行并重新编译的Ubuntu系统. 第二问:  什么场景下会用到armbian系统? 一个带有arm编译器 ...

  6. pt

    https://www.hdarea.co/torrents.php http://hdhome.org/torrents.php https://ourbits.club/torrents.php ...

  7. Python学习之--字符串的使用

    一.大小写转换 1. 首字母大写:title(); 如下: 2. 大写转换:upper(),如 3.小写转换:lower(),如 二.合并(拼接)字符串:”+“ 1. 2.  三.制表符.换行 制表符 ...

  8. 飞扬的小鸟 DP

    飞扬的小鸟 DP 细节有点恶心的DP,设\(f[i][j]\)表示横坐标为\(i\)(从\(0\)开始)高度为\(j\)时,屏幕点击的最小次数为\(f[i][j]\),转移便很好写了,这里要注意枚举当 ...

  9. jvm指令手册查看

    00-JVM指令手册 栈和局部变量操作 将常量压入栈的指令 aconst_null 将null对象引用压入栈 iconst_m1 将int类型常量-1压入栈 iconst_0 将int类型常量0压入栈 ...

  10. Java设计模式之一单例模式

    什么是单例模式 保证一个系统中的某个类只有一个实例而且该实例易于外界访问.例如Windows界面的任务管理器就可以看做是一个单例. 单例模式的使用场景 需要频繁的进行创建和销毁的对象: 创建对象时耗时 ...