Django的Meta类如何工作? [英] How does Django's Meta class work?

查看:109
本文介绍了Django的Meta类如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django,它允许人们使用 class Meta 添加额外的参数。

  class FooModel(models.Model):
...
class Meta:
...
/ pre>

我在Python的文档中找到的唯一的东西是:

  class FooMetaClass(type):
...

class FooClass:
__metaclass__ = FooMetaClass

但是,我不认为这是一样的。

解决方案

你正在问两个不同的问题:


  1. Meta Django模型中的内部类



    这只是一个类容器,它附有模型的一些选项(元数据)。它定义了可用的权限,关联的数据库表名称,模型是抽象的,单个和复数个版本的名称等。



    简短的解释在这里: Django文档:模型:元选项



    可用的元选项列表如下:


  2. Python中的元类



    最好的描述是这里:什么是Python中的元类?



I am using Django which allows people to add extra parameters to a class by using class Meta.

class FooModel(models.Model):
    ...
    class Meta:
        ...

The only thing I found in Python's documentation was:

class FooMetaClass(type):
    ...

class FooClass:
    __metaclass__ = FooMetaClass

However, I don't think this is the same thing.

解决方案

You are asking a question about two different things:

  1. Meta inner class in Django models:

    This is just a class container with some options (metadata) attached to the model. It defines such things as available permissions, associated database table name, whether the model is abstract or not, singular and plural versions of the name etc.

    Short explanation is here: Django docs: Models: Meta options

    List of available meta options is here: Django docs: Model Meta options

  2. Metaclass in Python:

    The best description is here: What is a metaclass in Python?

这篇关于Django的Meta类如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆