Inspecting objects

60 views
Skip to first unread message

Lukich

unread,
Jul 12, 2011, 1:25:57 PM7/12/11
to Django users
Hi. I have just started diving into Django and this question came up
- is there a way for me to examine all the attribute values of an
object? In Rails there's such a thing as debug statement which spits
out all the details about the object. From what I have seen so far in
Django, I can either do a __unicode__() trick, but it forces me to
specify which attribute to show. I also read about dir() and
__dict__, but they show me sets of attributes without their values. Is
there something I'm missing? Thank you!
Luka

Venkatraman S

unread,
Jul 12, 2011, 1:32:50 PM7/12/11
to django...@googlegroups.com
What would be the probable use-case?

Cal Leeming

unread,
Jul 12, 2011, 1:39:05 PM7/12/11
to django...@googlegroups.com
Ah, yeah I know what you mean. You want the PHP equivalent of "vardump".

I'm not aware of anything that does it, but it would be easy to make, by extending on dir:

def inspectobj(obj):
    return dict(map(lambda x: (x, getattr(obj, x)), dir(obj)))

pprint.pprint(inspectobj(django.db))
pprint.pprint(inspectobj(str))
pprint.pprint(inspectobj(django))

This is very very very basic, and you'd need to add all sorts of conditionals in there to make it extract the information you want in a proper, recursive fashion (and telling it how to deal with unbound methods etc), but it's a start.

Enjoy :)

Cal

 
Luka

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Cal Leeming [Simplicity Media Ltd]

unread,
Jul 12, 2011, 1:39:24 PM7/12/11
to django...@googlegroups.com
On Tue, Jul 12, 2011 at 6:25 PM, Lukich <luk...@gmail.com> wrote:

Cal Leeming [Simplicity Media Ltd]

unread,
Jul 12, 2011, 1:44:16 PM7/12/11
to django...@googlegroups.com
In fact, if anyone wants to extend on this concept, I'd be more than happy to provide some assistance :)

Andre Terra

unread,
Jul 12, 2011, 2:29:08 PM7/12/11
to django...@googlegroups.com
Useful, if the code is well documented: http://www.java2s.com/Code/Python/Utility/CheapandsimpleAPIhelper.htm

IIRC, use it like:

from apihelper import info
info(object, spacing=20)


Cheers,
André

Marc Aymerich

unread,
Jul 12, 2011, 3:52:47 PM7/12/11
to django...@googlegroups.com

Hi Luka,
I get the object attributes and their values with __dict__, It's not
enough for your case?

>>> Domain.objects.all()[1].__dict__
{'name_ptr_id': 2L, 'name': u'pangea', 'extension': u'org',
'slave_refresh': u'1d', 'min_caching_time': u'1h', '_state':
<django.db.models.base.ModelState object at 0x3351090>, 'is_mail':
False, 'slave_retry': u'2h', 'is_hosted': False, 'expire':
datetime.date(2012, 7, 31), 'active': True, 'is_purchased': False,
'slave_expiration': u'4w', 'id': 2L, 'serial': 203308L}

--
Marc

Andre Terra

unread,
Jul 12, 2011, 4:39:30 PM7/12/11
to django...@googlegroups.com
You should actually use dir() or vars() rather than __dict__.

Here's a thorough explanation, along with an example serializer which might need to access an object's attributes:

http://blog.enterthefoo.com/2008/08/pythons-vars.html



Regards,
André Terra

Lukich

unread,
Jul 12, 2011, 8:36:19 PM7/12/11
to Django users
Thank you all for your help!!!

Jirka Vejrazka

unread,
Jul 13, 2011, 2:25:12 AM7/13/11
to django...@googlegroups.com
> Thank you all for your help!!!

I know I'm a bit late to the party (blame timezones :), but thought
I'd put my 2 cents worth in :)

For inspecting models from command lines (usually when working with
models from legacy databases) I often use model_to_dict which is
"hidden" in django.forms.models.

>>> from django.forms.models import model_to_dict
>>> print model_to_dict(some_model)

HTH

Jirka

Jonas Geiregat

unread,
Jul 13, 2011, 2:32:01 AM7/13/11
to django...@googlegroups.com
I like to add:

import pdb; pdb.set_trace();

To my code.

Reload the page.

Go to the development server instance. You'll see that the page hangs forever ,since we started the python debugger.
Now you can easily inspect all variables, classes and functions from PDB.
Of course you're not limited to using set_trace().

Good luck !

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>


Jonas Geiregat
jo...@geiregat.org

Jonas Geiregat

unread,
Jul 13, 2011, 2:33:51 AM7/13/11
to django...@googlegroups.com

Op 13-jul-2011, om 08:25 heeft Jirka Vejrazka het volgende geschreven:

>> Thank you all for your help!!!
>
> I know I'm a bit late to the party (blame timezones :), but thought
> I'd put my 2 cents worth in :)
>
> For inspecting models from command lines (usually when working with
> models from legacy databases) I often use model_to_dict which is
> "hidden" in django.forms.models.

This is new for me. I've noticed before that django's API docs doesn't all function available. There's probably a good reason for it.

What's the advantage of using model_to_dict against dict(some_queryset) ?

Cal Leeming [Simplicity Media Ltd]

unread,
Jul 13, 2011, 7:58:02 AM7/13/11
to django...@googlegroups.com
You know, I think that this subject deserves it own page on the wiki. I'll get something started later :)

Cal Leeming [Simplicity Media Ltd]

unread,
Aug 11, 2011, 6:30:57 AM8/11/11
to django...@googlegroups.com
Temporarily added to this to
https://code.djangoproject.com/wiki/UsingTheMailingList

Can be split into its own page another day

Reply all
Reply to author
Forward
0 new messages