django - Get an attribute's class from its name in Python? -
how can class attribute's type in python same way in java code?
field f = classname.class.getdeclaredfield("attr_name"); class<?> ftype = f.gettype();
with
c = classname() c.__dict__
i can attributes name , values, not types.
more specifically, using django, if have 2 classes, poll , choice, defined 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 know if class has models.foreignkey
attribute's type , other class points?
i need output like:
# attribute poll in class choice foreignkey type # , points poll model choice.poll -> poll
to class name of related model, use rel.to
:
print choice.poll.field.rel.to.__name__ # prints 'poll'
or, way:
field = choice._meta.get_field_by_name('poll')[0] print field.rel.to.__name__ # prints 'poll'
also see:
Comments
Post a Comment