简体   繁体   English

Django 型号。Model class。 模型.CharField

[英]Django models.Model class. models.CharField

This question might have been asked before, but sorry I couldn't find it so that is why I am asking这个问题之前可能已经被问过,但很抱歉我找不到它,所以这就是我问的原因

I want to understand the title models.CharField()我想了解标题models.CharField()

I understand that the we inherit the Model class from the module call models.我知道我们从模块调用模型继承了 Model class 。

but then why not the class Model.charfield() as the method or function of Model但是为什么不使用 class Model.charfield()作为方法或 ZA559B87068925EEC784 的 function

why models?为什么是模型? which seams to me like a module and CharField seems like a class这对我来说就像一个模块,而CharField看起来像一个 class

its seems to me we are accessing a class from the models module.在我看来,我们正在从模型模块访问 class 。

example例子

from django.db import models

class Page(models.Model):
    title = models.CharField(max_length=60)
    permalink = models.CharField(max_length=12, unique=True)
    update_date = models.DateTimeField('Last Updates')
    bodytext = models.TextField('Page Content', blank=True)

I appreciate your time我很感激你的时间

Model inherit from models class, In django ORM Consider it more Like Model is to create a table in database and for each columns(here fields) in database we use models.corresponding_fields() Model inherit from models class, In django ORM Consider it more Like Model is to create a table in database and for each columns(here fields) in database we use models.corresponding_fields()

if you inspect db.models:如果您检查 db.models:

    (Many imports and )
    from django.db.models.query_utils import FilteredRelation

    # Imports that would create circular imports if sorted
    from django.db.models.base import DEFERRED, Model  # isort:skip
    from django.db.models.fields.related import (  # isort:skip
        ForeignKey, ForeignObject, OneToOneField, ManyToManyField,
        ManyToOneRel, ManyToManyRel, OneToOneRel,
    )


    __all__ = aggregates_all + constraints_all + enums_all + fields_all + indexes_all
    __all__ += [
        'ObjectDoesNotExist', 'signals',
        'CASCADE', 'DO_NOTHING', 'PROTECT', 'SET', 'SET_DEFAULT', 'SET_NULL',
        'ProtectedError',
        'Case', 'Exists', 'Expression', 'ExpressionList', 'ExpressionWrapper', 'F',
        'Func', 'OuterRef', 'RowRange', 'Subquery', 'Value', 'ValueRange', 'When',
        'Window', 'WindowFrame',
        'FileField', 'ImageField', 'OrderWrt', 'Lookup', 'Transform', 'Manager',
        'Prefetch', 'Q', 'QuerySet', 'prefetch_related_objects', 'DEFERRED', 'Model',
        'FilteredRelation',
        'ForeignKey', 'ForeignObject', 'OneToOneField', 'ManyToManyField',
        'ManyToOneRel', 'ManyToManyRel', 'OneToOneRel',
    ]

in short models in just __init__.py file contains all imports as a collection简而言之,仅__init__.py文件中的模型包含所有导入作为集合

for in depth details you can reads django docs - Model有关详细信息,您可以阅读django 文档 - Model

for any one else worried about this...对于其他为此担心的人...

i wasn't wrong to ask this... and here i got my answer from django documentation page我问这个没有错...在这里我从 django 文档页面得到了答案

click [here] ( https://docs.djangoproject.com/en/3.1/ref/models/fields/ )点击[这里]( https://docs.djangoproject.com/en/3.1/ref/models/fields/

Note笔记

Technically, these models are defined in django.db.models.fields, but for convenience they're imported into django.db.models;从技术上讲,这些模型在 django.db.models.fields 中定义,但为方便起见,它们被导入到 django.db.models 中; the standard convention is to use from django.db import models and refer to fields as models.Field.标准约定是使用 from django.db 导入模型并将字段称为 models.Field。

this the answer: Technically, these models are defined in django.db.models.fields, but for convenience they're imported into django.db.models这就是答案:从技术上讲,这些模型在 django.db.models.fields 中定义,但为方便起见,它们被导入 django.db.models

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

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