zoukankan      html  css  js  c++  java
  • Django U2 模型

     http://python.usyiyi.cn/translate/django_182/topics/db/models.html

    每个元组中的第一个元素,是存储在数据库中的值;第二个元素是在管理界面或 ModelChoiceField 中用作显示的内容。 在一个给定的 model 类的实例中,想得到某个 choices 字段的显示值,就调用 get_FOO_display 方法(这里的 FOO 就是 choices 字段的名称 )。例如:

    from django.db import models
    
    class Person(models.Model):
        SHIRT_SIZES = (
            ('S', 'Small'),
            ('M', 'Medium'),
            ('L', 'Large'),
        )
        name = models.CharField(max_length=60)
        shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZES)
    >>> p = Person(name="Fred Flintstone", shirt_size="L")
    >>> p.save()
    >>> p.shirt_size
    'L'
    >>> p.get_shirt_size_display()
    'Large'
    

      

    字段的自述名

    ForeignKeyManyToManyField 和 OneToOneField 之外,每个字段类型都接受一个可选的位置参数 —— 字段的自述名。如果没有给定自述名,Django 将根据字段的属性名称自动创建自述名 —— 将属性名称的下划线替换成空格。

    在这个例子中,自述名是 "person's first name":

    first_name = models.CharField("person's first name", max_length=30)
    

    在这个例子中,自述名是  "first name"

    first_name = models.CharField(max_length=30)
    

    ForeignKeyManyToManyField 和 OneToOneField 都要求第一个参数是一个模型类,所以要使用 verbose_name 关键字参数才能指定自述名:

    poll = models.ForeignKey(Poll, verbose_name="the related poll")
    sites = models.ManyToManyField(Site, verbose_name="list of sites")
    place = models.OneToOneField(Place, verbose_name="related place")
    

    习惯上,verbose_name 的首字母不用大写。Django 在必要的时候会自动大写首字母。

  • 相关阅读:
    Ubuntu 16.04
    每天一道LeetCode--389. Find the Difference
    每天一道LeetCode--371. Sum of Two Integers
    Ubuntu 16.04 小飞机启动失败
    每天一道LeetCode--344. Reverse String
    leetcode1458 Max Dot Product of Two Subsequences
    CF1313C2 Skyscrapers (hard version)
    CF1295C Obtain The String
    CF1251D Salary Changing
    CF1286A Garland
  • 原文地址:https://www.cnblogs.com/zsr0401/p/6394416.html
Copyright © 2011-2022 走看看