how can i introspect properties and model fields in django?

Posted by shreddd on Stack Overflow See other posts from Stack Overflow or by shreddd
Published on 2011-02-08T07:00:00Z Indexed on 2011/02/08 7:25 UTC
Read the original article Hit count: 118

Filed under:
|
|

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 and properties.

class MyModel(Model)
    url = models.TextField()

    def _get_location(self):
        return "%s/jobs/%d"%(url, self.id)

    location = property(_get_location)

What I want is something that returns a dict that looks like this:

{
  'id' : 1,
  'url':'http://foo',
  'location' : 'http://foo/jobs/1'
}   

I can use model._meta.fields to get the model fields, but this doesn't give me things that are properties but not real DB fields.

© Stack Overflow or respective owner

Related posts about python

Related posts about django