【问题标题】:how to get model name that has m2m relation to a specific model如何获取与特定模型具有 m2m 关系的模型名称
【发布时间】:2015-07-02 07:30:05
【问题描述】:

假设我有两个模型 A 和 B:

A 类(models.Model):

 name = models.CharField()

B 类(models.Model):

 field = models.ManyToManyField(A)

如何从字符串 A 中获取模型名称 B 和字段?或者如何从字符串 B 中获取模型名称 A 和字段?两者都可以。

【问题讨论】:

    标签: django-models


    【解决方案1】:

    试试这个:

    B._meta.get_field('field').related_model._meta.model_name
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2011-07-04
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多