Skip to content

Instantly share code, notes, and snippets.

@majgis
Created September 18, 2012 15:43
Show Gist options
  • Save majgis/3743833 to your computer and use it in GitHub Desktop.
Save majgis/3743833 to your computer and use it in GitHub Desktop.
django snippets
#Compact example to get the name of the primary key field of a Django model.
loc_fields = self._model._meta.fields
loc_pk = next((f.db_column or f.name for f in loc_fields if f.primary_key))
#Get all related model classes for a model class or instance
related_models = [f.rel.to for f in loc_record._meta.fields if f.rel]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment