How can I get class attribute's type in Python the same way I do it in Java with this code?
Field f = ClassName.class.getDeclaredField("attr_name");
Class<?> ftype = f.getType();
With
c = ClassName()
c.__dict__
I can get all attributes name and values, but not their types.
More specifically, using Django, if I have two classes, say Poll and Choice, defined as follow
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
How can I know if a class has a models.ForeignKey
as attribute's type and to which other class it points?
I need as output something like:
# attribute poll in class Choice is a ForeignKey type
# and it points to Poll model
Choice.poll -> Poll