【问题标题】:Get m2m field (with through model) list in django在 django 中获取 m2m 字段(通过模型)列表
【发布时间】:2019-08-09 21:04:45
【问题描述】:

我正在使用 Django 2.0.7。

我想获取模型的所有字段。我可以在以下位置获取所有正常字段:

model._meta.fields

我可以在以下位置获取 m2m 字段:

model._meta.local_many_to_many

但是,如果 m2m 字段有一个直通表,我无法访问它们?我怎样才能做到这一点?

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    m2m 字段有一个您可以访问的through 模型:

    YourModel.m2m_field.through._meta.fields
    

    【讨论】:

      【解决方案2】:

      你试过了吗?

      model._meta.get_all_field_names()
      

      【讨论】:

      • “get_all_field_names”方法不是 _meta 的属性......不在我的版本中
      猜你喜欢
      • 2016-09-03
      • 2022-01-22
      • 2020-03-30
      • 2011-03-07
      • 2015-06-03
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2011-03-07
      相关资源
      最近更新 更多