I am using django version 1.0 and ran into shallow copy limitation of
QuerySet.values() where it returns ints for ForeignKey columns. Here
is my take on deep copy implementation.
def deep_values(o, *args, **kargs):
""" Converts instance of django.db.models object into dictionary.
Uses recursive deep copy to overcome ModelManager.values() shallow
copy.
"""
if not isinstance(o, models.Model):
return None
dict = {}
lead_space = 2
c_nlevel = kargs.get('nest_level', 0)
attribs = [attr for attr in dir(o) if not attr in args and not
attr.startswith('_') and not attr == 'objects' and not callable(getattr
(o, attr)) and not attr == 'pk' and not attr.endswith('_id')]
for a in attribs:
if not repr(type(getattr(o, a))).startswith("<class"):
dict[a] = getattr(o, a)
else:
import logging
logging.debug("%srecursive call for attribute '%s' of
%s", ' ' *
lead_space * c_nlevel, a, o.__class__)
tmp = deep_values(getattr(o, a), args,
nest_level=c_nlevel + 1)
if not tmp is None:
dict[a] = tmp
return dict
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---