django-枚举类型扩展方法

时间:2024-01-22 10:59:45

django-枚举类型扩展方法

原文地址:https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display

关于django中枚举类型转换显示问题,每次设置枚举类型,

EXPERIENCE_CHOICES = (
(1, '应届毕业生'),
(2, '3年及以下'),
(3, '3-5年'),
(4, '5-10年'),
(5, '10年以上'),
)

数据库实际存储值为,1,2,3, 4, 5

[
  {
    "id": 2,
    "name": "Python开发工程师",
    "salary": 3,
    "experience": 2,
    "education": 2,
    "type": 1,
    "create_time": "2018-12-11T10:48:03.076841+08:00",
    "enterprise": {
      "id": 1,
      "name": "智客网",
      "city": {
        "id": 1,
        "name": "北京"
      }
    }
  },
  {
    "id": 1,
    "name": "Python研发工程师",
    "salary": 4,
    "experience": 2,
    "education": 2,
    "type": 1,
    "create_time": "2018-12-11T10:47:03.898845+08:00",
    "enterprise": {
      "id": 2,
      "name": "Gridsum 国双",
      "city": {
        "id": 1,
        "name": "北京"
      }
    }
  }
]

 中括号为了区别django模板语句,在delimiters: ['[[', ']]']

let vm = new Vue({
    el: '#app',
    delimiters: ['[[', ']]'], // 修改vue模板符号,防止与django冲突
    data: {
        host: host,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        new_jobs: [], // 最新职位
        hot_jobs: [], // 热门职位
    mounted: function(){

在html页面使用obj.get_字段名称_display

[[job.get_salary_display]]([[job.salary]])

 

posted on 2018-12-11 12:11 new_candy 阅读(...) 评论(...) 编辑 收藏