Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Last active August 29, 2015 14:08
Show Gist options
  • Save kezabelle/bab4426a8d6746303f85 to your computer and use it in GitHub Desktop.
Save kezabelle/bab4426a8d6746303f85 to your computer and use it in GitHub Desktop.
getting everything but deferred attributes for a given Django model. Because otherwise, doing getattr() forces evaluation of deferred fields.
# see https://github.com/django/django/blob/c32bc1a7a7bbb3d5bd0a2c11bc77dd5ab1c32fbc/django/db/models/base.py#L415
# and http://stackoverflow.com/questions/4523651/how-to-tell-which-fields-have-been-deferred-onlyd-in-a-django-queryset
concrete_fieldnames = (x.attname for x in obj._meta.concrete_fields)
non_deferred = dict(
(attr, getattr(obj, attr))
for attr in concrete_fieldnames
if not isinstance(obj.__class__.__dict__.get(attr), DeferredAttribute)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment