Skip to content

Instantly share code, notes, and snippets.

@gcavalcante8808
Created September 3, 2014 14:53
Show Gist options
  • Save gcavalcante8808/bf36ef2aa32d110ddd18 to your computer and use it in GitHub Desktop.
Save gcavalcante8808/bf36ef2aa32d110ddd18 to your computer and use it in GitHub Desktop.
A simple recipe to read objectGUID field from an active directory object on django-ldapdb.
from ldapdb.models.fields import CharField
from uuid import UUID
def guid2string(val):
""" convert an active directory binary objectGUID value as returned by
python-ldap into a string that can be used as an LDAP query value """
s = UUID(bytes_le=val)
return s
class GUIDField(CharField):
"""
Field with support for Binary objectGUID support on ActiveDirectory.
"""
def from_ldap(self, value, connection):
if len(value) == 0:
return ''
else:
return guid2string(value[0])
class LdapComputer(ldapdb.models.Model):
base_dn = "dc=X,dc=X"
object_classes = ['computer']
id = GUIDField(db_column='objectGUID', unique=True, primary_key=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment