django中Meta类有什么用?

问题描述 投票:0回答:3

我一直在用

django
并且用了很多次
class Meta:
,其实它有什么用呢?

例如,在django中

models

class Accounts(models.Model):

   ---some code here--- 

   class Meta:
       ordering = [-1]

在 Django 中

forms

class AccountForm(forms.ModelForm):

   ---some code here--- 

   class Meta:
       fields = '__all__'
python django django-models django-rest-framework django-forms
3个回答
0
投票

class Meta
基本上是内部类。在 Django 中,使用
Meta
类只是为了向
ModelForm
Model
类提供元数据。它与 Python 中类的
metaclass
不同。


0
投票

class Meta
用于更改模型的行为,例如排序、verbose_name 等。尽管它是可选的,是否包含在模型中。


0
投票

class meta
是可选的。但是通过使用
class meta
你只是简单地将
metadata
传递给
class model
modelform

© www.soinside.com 2019 - 2024. All rights reserved.