Can i get models field type from a model queryset in Django?

Viewed 9474

Can i get model field type from a model queryset in Django?

For example: a is b model's queryset and the b model has following fields:

  • f:charfield
  • g:foreignkey
  • h:manytomany

Is there any way to get field g's type from queryset a? thx.

2 Answers
fields = [f.attname for f in Experience._meta.fields]
file_fields = []
print("FILE FIELDS : ", fields)
for field in fields:
    get_type = Experience._meta.get_field(field)
    print("GET TYPE :", get_type)
    field_type = get_type.__class__.__name__
    print(field, "FILE TYPE : ", field_type)
    if field_type == "FileField":
        file_fields.append(field)

This code can get the field types of django models.

Related