• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孙龙 程序员
少时总觉为人易,华年方知立业难
博客园    首页    新随笔    联系   管理    订阅  订阅
字段访问API-get_field
de style="margin: 0px; padding: 0px; font-weight: normal; line-height: 15.4px;" >Options.de>de style="margin: 0px; padding: 0px; font-weight: normal; line-height: 15.4px;" >get_fieldde>(field_name)[source]

返回给定字段名称的字段实例。

de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >field_namede>可以是模型上的字段名称,抽象或继承模型上的字段,或在指向模型的另一个模型上定义的字段。 在后一种情况下,de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >field_namede>将是由用户定义的de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >related_namede>或由Django本身自动生成的名称。

de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Hidden fieldsde>。

如果找不到具有给定名称的字段,则会引发de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >FieldDoesNotExistde>异常。

>>> from django.contrib.auth.models import User

# A field on the model
>>> User._meta.get_field('username')
<django.db.models.fields.CharField: username>

# A field from another model that has a relation with the current model
>>> User._meta.get_field('logentry')
<ManyToOneRel: admin.logentry>

# A non existent field
>>> User._meta.get_field('does_not_exist')
Traceback (most recent call last):
    ...
FieldDoesNotExist: User has no field named 'does_not_exist'

Retrieving all field instances of a model

de style="margin: 0px; padding: 0px; font-weight: normal; line-height: 15.4px;" >Options.de>de style="margin: 0px; padding: 0px; font-weight: normal; line-height: 15.4px;" >get_fieldsde>(include_parents=True, include_hidden=False)[source]

返回与模型关联的字段的元组。 de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >get_fields()de>接受两个参数,可用于控制返回的字段:

de style="margin: 0px; padding: 0px; font-weight: normal; line-height: 15.4px; color: rgb(35, 79, 50); white-space: nowrap; background: none;" >include_parentsde>
de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Truede>。 递归地包括在父类上定义的字段。 如果设置为de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Falsede>,de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >get_fields()de>将只搜索在当前模型上直接声明的字段。 来自直接从抽象模型或代理类继承的模型的字段被认为是本地的,而不是父级的。
de style="margin: 0px; padding: 0px; font-weight: normal; line-height: 15.4px; color: rgb(35, 79, 50); white-space: nowrap; background: none;" >include_hiddende>
de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Falsede>。 如果设置为de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Truede>,则de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >get_fields()de>将包含用于返回其他字段功能的字段。 这还将包括具有以“+”开头的de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >related_namede>(例如de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >ManyToManyFieldde>或de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >ForeignKeyde>)的任何字段。
>>> from django.contrib.auth.models import User
>>> User._meta.get_fields()
(<ManyToOneRel: admin.logentry>,
 <django.db.models.fields.AutoField: id>,
 <django.db.models.fields.CharField: password>,
 <django.db.models.fields.DateTimeField: last_login>,
 <django.db.models.fields.BooleanField: is_superuser>,
 <django.db.models.fields.CharField: username>,
 <django.db.models.fields.CharField: first_name>,
 <django.db.models.fields.CharField: last_name>,
 <django.db.models.fields.EmailField: email>,
 <django.db.models.fields.BooleanField: is_staff>,
 <django.db.models.fields.BooleanField: is_active>,
 <django.db.models.fields.DateTimeField: date_joined>,
 <django.db.models.fields.related.ManyToManyField: groups>,
 <django.db.models.fields.related.ManyToManyField: user_permissions>)

# Also include hidden fields.
>>> User._meta.get_fields(include_hidden=True)
(<ManyToOneRel: auth.user_groups>,
 <ManyToOneRel: auth.user_user_permissions>,
 <ManyToOneRel: admin.logentry>,
 <django.db.models.fields.AutoField: id>,
 <django.db.models.fields.CharField: password>,
 <django.db.models.fields.DateTimeField: last_login>,
 <django.db.models.fields.BooleanField: is_superuser>,
 <django.db.models.fields.CharField: username>,
 <django.db.models.fields.CharField: first_name>,
 <django.db.models.fields.CharField: last_name>,
 <django.db.models.fields.EmailField: email>,
 <django.db.models.fields.BooleanField: is_staff>,
 <django.db.models.fields.BooleanField: is_active>,
 <django.db.models.fields.DateTimeField: date_joined>,
 <django.db.models.fields.related.ManyToManyField: groups>,
 <django.db.models.fields.related.ManyToManyField: user_permissions>)

Migrating from the old API

作为de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Model._metade> API(从de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >django.db.models.options.Optionsde>类)的形式化的一部分,许多方法和属性已被弃用,将在Django 1.10中删除。

这些旧的API可以复制:

  • 调用de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Options.get_field()de>或;
  • 调用de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >Options.get_fields()de>以检索所有字段的列表,然后使用field attributes过滤此列表(或在de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >_with_modelde> variant)所需字段的属性。

虽然可以对旧方法进行严格等同的替换,但这可能不是最好的方法。 花费时间重构任何字段循环以更好地使用新的API - 并且可能包括先前被排除的字段 - 几乎肯定会导致更好的代码。

假设您有一个名为de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModelde>的模型,可以进行以下替换,将代码转换为新的API:

  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_field(name)de>变成:

    f = MyModel._meta.get_field(name)
    

    然后检查:

    • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >f.auto_created == Falsede>,因为新的de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >get_field()de>找到“反向”关系,并且:
    • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >f.is_relation 和 f.related_model 无 t0 >,因为新的de style="margin: 0px; padding: 0px; line-height: 14px;" >get_field()de> API将找到de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50);" >GenericForeignKeyde>关系。de>
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_field_by_name(name)de>使用以下替换返回这四个值的元组:

    • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >fieldde>可以通过de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_field(name)de>

    • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >modelde>可以通过字段上的de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >modelde>属性找到。

    • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >directde>可以通过以下方式找到:de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >不 field.auto_created 或 field.concretede>

      de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >auto_createdde>检查排除由Django创建的所有“前进”和“反向”关系,但这也包括代理模型上的de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >AutoFieldde>和de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >OneToOneFieldde> 。 我们避免使用de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >concretede>属性过滤这些属性。

    • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >m2mde>可以通过字段上的de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >many_to_manyde>属性找到。

  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_fields_with_model()de>变成:

    [
        (f, f.model if f.model != MyModel else None)
        for f in MyModel._meta.get_fields()
        if not f.is_relation
            or f.one_to_one
            or (f.many_to_one and f.related_model)
    ]
    
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_concrete_fields_with_model()de>变成:

    [
        (f, f.model if f.model != MyModel else None)
        for f in MyModel._meta.get_fields()
        if f.concrete and (
            not f.is_relation
            or f.one_to_one
            or (f.many_to_one and f.related_model)
        )
    ]
    
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_m2m_with_model()de>变成:

    [
        (f, f.model if f.model != MyModel else None)
        for f in MyModel._meta.get_fields()
        if f.many_to_many and not f.auto_created
    ]
    
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_all_related_objects()de>变成:

    [
        f for f in MyModel._meta.get_fields()
        if (f.one_to_many or f.one_to_one)
        and f.auto_created and not f.concrete
    ]
    
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_all_related_objects_with_model()de>变成:

    [
        (f, f.model if f.model != MyModel else None)
        for f in MyModel._meta.get_fields()
        if (f.one_to_many or f.one_to_one)
        and f.auto_created and not f.concrete
    ]
    
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_all_related_many_to_many_objects()de>变成:

    [
        f for f in MyModel._meta.get_fields(include_hidden=True)
        if f.many_to_many and f.auto_created
    ]
    
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_all_related_m2m_objects_with_model()de>变成:

    [
        (f, f.model if f.model != MyModel else None)
        for f in MyModel._meta.get_fields(include_hidden=True)
        if f.many_to_many and f.auto_created
    ]
    
  • de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >MyModel._meta.get_all_field_names()de>变成:

    from itertools import chain
    list(set(chain.from_iterable(
        (field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
        for field in MyModel._meta.get_fields()
        # For complete backwards compatibility, you may want to exclude
        # GenericForeignKey from the results.
        if not (field.many_to_one and field.related_model is None)
    )))
    

    这提供了100%向后兼容的替换,确保包括字段名称和属性名称de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >ForeignKeyde>,但与de style="margin: 0px; padding: 0px; line-height: 14px; color: rgb(35, 79, 50); white-space: nowrap;" >GenericForeignKeyde>相关联的字段不包括。 一个更简单的版本是:

    [f.name for f in MyModel._meta.get_fields()]
    

    虽然这不是100%向后兼容,但在许多情况下可能就足够了。

本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/8691990.html

posted on 2018-04-02 11:44  孙龙-程序员  阅读(198)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3