Package: django-ldapdb2
django-ldapdbis an LDAP database backend for Django, allowing to manipulate
LDAP entries through Django models.It supports most of the same APIs as a Django model:MyModel.objects.create()MyModel.objects.filter(x=1, y__contains=2)Full admin support and browsingdjango-ldapdbsupports every upstream-supported Django version, based on
theDjango support policy.For the current version, the following versions are supported:Django 2.2 (LTS), under Python 3.6 - 3.8 (Python 3.5 has reached its end of life);Django 3.0, under Python 3.6 - 3.8;Django 3.1, under Python 3.6 - 3.8.Installing django-ldapdbLinuxUse pip:pip installdjango-ldapdbYou might also need the usualLDAPpackages from your distribution, usually namedopenldaporldap-utils.Windowsdjango-ldapdbdepends on thepython-ldap <https://pypi.python.org/pypi/python-ldap>project.
Either followits Windows installation guide,
or install a pre-built version fromhttps://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap(choose the.whlfile matching your Python/Windows combination, and install it withpip installpython-ldap-3...whl).You may then installdjango-ldapdbwithpip installdjango-ldapdbUsing django-ldapdbAdd the following to yoursettings.py:DATABASES={'ldap':{'ENGINE':'ldapdb.backends.ldap','NAME':'ldap://ldap.nodomain.org/','USER':'cn=admin,dc=nodomain,dc=org','PASSWORD':'some_secret_password',},'default':{'ENGINE':'django.db.backends.sqlite3','NAME':os.path.join(BASE_DIR,'db.sqlite3'),},}DATABASE_ROUTERS=['ldapdb.router.Router']If you want to access posixGroup entries in your application, you can add
something like this to yourmodels.py:fromldapdb.models.fieldsimportCharField,IntegerField,ListFieldimportldapdb.modelsclassLdapGroup(ldapdb.models.Model):"""
    Class for representing an LDAP group entry.
    """# LDAP meta-database_dn="ou=groups,dc=nodomain,dc=org"object_classes=['posixGroup']# posixGroup attributesgid=IntegerField(db_column='gidNumber',unique=True)name=CharField(db_column='cn',max_length=200,primary_key=True)members=ListField(db_column='memberUid')def__str__(self):returnself.namedef__unicode__(self):returnself.nameand add this to youradmin.py:fromdjango.contribimportadminfrom.importmodelsclassLDAPGroupAdmin(admin.ModelAdmin):exclude=['dn','objectClass']list_display=['gid','name']admin.site.register(models.LDAPGroup,LDAPGroupAdmin)Important note:Youmustdeclare an attribute to be used as the primary key.
This attribute will play a special role, as it will be used to build
the Relative Distinguished Name of the entry.For instance in the example above, a group whose cn isfoowill have the DNcn=foo,ou=groups,dc=nodomain,dc=org.Supported fieldsdjanglo-ldapdb provides the following fields, all imported fromldapdb.models.fields:Similar to Django:IntegerFieldFloatFieldBooleanFieldCharFieldImageFieldDateTimeFieldSpecific to a LDAP server:ListField(holds a list of text values)TimestampField(Stores a datetime as a posix timestamp, typically for posixAccount)Legacy:DateField(Stores a date in an arbitrary format. A LDAP server has no notion ofDate).Tuning django-ldapdbIt is possible to adjust django-ldapdb’s behavior by defining a few parameters in theDATABASEsection:PAGE_SIZE(default:1000)Define the maximum size of a results page to be returned by the serverQUERY_TIMEOUT(default: no limit)Define the maximum time in seconds we’ll wait to get a reply from the server (on a per-query basis).NoteThis setting applies on individual requests; if a high-level operation requires many
queries (for instance a paginated search yielding thousands of entries),
the timeout will be used on each individual request;
the overall processing time might be much higher.Developing with a LDAP serverWhen developing against a LDAP server, having access to a development LDAP server often proves
useful.django-ldapdb uses thevolatildap projectfor this purpose:A LDAP server is instantiated for each TestClass;Its content is reset at the start of each test function;It can be customized to embark any schemas required by the application;Starting with volatildap 1.4.0, the volatildap server can be controlled remotely, avoiding the need
to install a LDAP server on the host.Applications using django-ldapdb may use the following code snippet when setting up their tests:# This snippet is released in the Public Domainfromdjango.confimportsettingsfromdjango.testimportTestCaseimportvolatildapclassLdapEnabledTestCase(TestCase):@classmethoddefsetUpClass(cls):super().setUpClass()cls.ldap=volatildap.LdapServer(# Load some initial datainitial={'ou=people':{'ou':['people'],'objectClass':['organizationalUnit'],}},# Enable more LDAP schemasschemas=['core.schema','cosine.schema','inetorgperson.schema','nis.schema'],)# The volatildap server uses specific defaults, and listens on an arbitrary port.# Copy the server-side values to Django settingssettings.DATABASES['ldap']['USER']=cls.ldap.rootdnsettings.DATABASES['ldap']['PASSWORD']=cls.ldap.rootpwsettings.DATABASES['ldap']['NAME']=cls.ldap.uridefsetUp(self):super().setUp()# Starting an already-started volatildap server performs a data resetself.ldap.start()@classmethoddeftearDownClass(cls):# Free up resources on teardown.cls.ldap.stop()super().tearDownClass()