django shell-plus:如何从命令行查看模型定义

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

我正在使用Django Rest Framework,我遇到了repr()函数

当我在Django shell中

>> from articles.api.serializers import PostSerializer
   print(repr(PostSerializer()))

PostSerializer():
    id = IntegerField(label='ID', read_only=True)
    publish_date = DateTimeField(format='%Y-%m-%d %H:%M UTC')
    is_deleted = BooleanField(required=False)
    title = CharField(max_length=256)
    intro_image = ImageField(allow_null=True, max_length=100, required=False)
    slug = SlugField(max_length=50)
    task_id = CharField(read_only=True)
    qa_bool = BooleanField(label='Allow Q&A', required=False)

假设我有模型文章。我试过了:

>> print(repr(Article))

<class 'articles.models.Article'>

我想从shell中看到所有带有属性的字段。可能吗

django django-rest-framework
1个回答
0
投票

您可以像这样使用Django的_meta Api:

>>> Article._meta.get_fields()
<<< { tuple with all fields and types }
© www.soinside.com 2019 - 2024. All rights reserved.