我可以从模型查询集中获取相关字段的类型吗?
让我们考虑示例模型:
class Semester(models.Model):
active = models.BooleanField(default=False, verbose_name="Active")
class Subject(models.Model):
name = models.CharField(max_length=100, verbose_name="Name")
semester = models.ForeignKey(Semester, verbose_name="Semester")
如果我在变量和查询集中有一些字段名,我可以这样做:
querySet = Subject.objects.all()
some_field_name = 'name'
field_type = querySet.model._meta.get_field(some_field_name).get_internal_type()
有没有办法获取相关的字段类型,例如:
querySet = Subject.objects.all()
some_field_name = 'semester__active'
field_type = ?