如何从Django中的ManyToManyField获取类模型名称

时间:2011-02-18 13:33:03

标签: django many-to-many metamodel

我需要从#django.db.models.fields.related.ManyToManyField对象获取类模型名称。例如:

class Source(models.Model):
    groups = models.ManyToManyField(Group)

    def generated_sql(self):
        print [f.name for f in self._meta.many_to_many]

        #tehere I need to get Class Model name for f, in this case will be: Group

提前致谢

1 个答案:

答案 0 :(得分:2)

groups = models.ManyToManyField(Group)

def generated_sql(self):
    print [(f.name, f.related.parent_model) for f in self._meta.many_to_many]

iPython ftw