简体   繁体   English

在django中使用元类

[英]Use of meta class in django

Can someone please explain why is meta class used in the following example. 有人可以解释为什么在以下示例中使用元类。

Ex: 例如:

Class Employee (models.Model):
    name = models.ForeignKey(name)
    Gender = models.IntegerField()


    class Meta:
        ordering = ["Gender"]

Thanks. 谢谢。

Django models use the Meta class to contain extra information about the model that would not necessarily be appropriate to contain within the model class itself. Django模型使用Meta类来包含有关模型的额外信息,这些信息不一定适合包含在模型类本身中。 Note that this is not the same as Python's metaclass ; 请注意,这是一样的Python的metaclass ; that is a completely different topic. 这是一个完全不同的话题。

In this case it ordering or sorting the queries to this model by field "Gender" 在这种情况下,它通过字段“Gender”对此模型进行排序或排序

因为作者/程序员想要按性别字段的值对结果进行排序。

在这种情况下,如果您未在查询中提供ORDER_BY ,它将定义用于排序的默认字段。

It is explained in Django Documentation for Models 它在Django Documentation for Models中进行了解释

https://docs.djangoproject.com/en/dev/topics/db/models/

Give your model metadata by using an inner class Meta, like: 使用内部类Meta提供模型元数据,例如:

Class Employee (models.Model):
    ....

    class Meta:
        ordering = ["attribute-X"]

Another useful option can be used in class Meta is verbose_name. 另一个有用的选项可以在类Meta中使用verbose_name。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM