How can I introspect properties and model fields in Django?

后端 未结 4 1898
长发绾君心
长发绾君心 2021-01-05 02:55

I am trying to get a list of all existing model fields and properties for a given object. Is there a clean way to instrospect an object so that I can get a dict of fields an

4条回答
  •  走了就别回头了
    2021-01-05 03:31

    class Awesome(models.Model):
     foo = models.TextField()
     bar = models.CharField(max_length = 200)
    
    awe = Awesome()
    for property in awe.__dict__.copy():
     # pass private properties.
     if not property.startswith('_'):
      print property,getattr(awe,property)
    

    This is how they do it in Django.

提交回复
热议问题