• Django实践:个人博客系统(第七章 Model的设计和使用)


    model中定义一个类,如果不在Meta中指明verbose_name,在admin页面中默认显示类名。如:

    class Publisher(models.Model):
        name = models.CharField(max_length=30)
        address = models.CharField(max_length=50, blank=True)
        city = models.CharField(max_length=60)
        state_province = models.CharField(max_length=30, blank=True)
        country = models.CharField(max_length=50)
        website = models.URLField(blank=True)
    
        class Meta:
            verbose_name = '出版者'
            verbose_name_plural = verbose_name
    
        def __unicode__(self):
            return self.name
    
    
    class Author(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=40)
        email = models.EmailField()
    
        class Meta:
            verbose_name = '作者'
            verbose_name_plural = verbose_name
    
        def __unicode__(self):
            return self.first_name
    
    
    class Book(models.Model):
        title = models.CharField(max_length=100)
        authors = models.ManyToManyField(Author, blank=True)
        publisher = models.ForeignKey(Publisher, blank=True)
        publication_date = models.DateField(blank=True)
    
        #class Meta:
        #    verbose_name = '书名'
        #    verbose_name_plural = verbose_name
    
        def __unicode__(self):
            return self.title

    在admin中显示情况:

    在字段中的verbose_name应该同理,如:

    class Publisher(models.Model):
        name = models.CharField(max_length=30,verbose_name = '姓名')
        address = models.CharField(max_length=50, blank=True)

    此时一个Publisher的实例在admin中显示的name字段应该为verbose_name的值姓名,但在这里代码中verbose_name并没有写。

  • 相关阅读:
    ceph
    分布式网关层
    function declarations are hoisted and class declarations are not 变量提升
    js为Object对象动态添加属性和值 eval c.k c[k]
    方法就是一种变量
    static 不被实例调用
    WePY根据环境变量来改变运行时的参数
    函数类型实现接口——把函数作为接口来调用
    为什么需要onRoute函数?
    504 Gateway Timeout Error 502 Bad Gateway
  • 原文地址:https://www.cnblogs.com/songbird/p/5140530.html
Copyright © 2020-2023  润新知