Django with LDAP Integration. Primary LDAP DB for Django. User Creation on LDAP via Django

I am trying to Integrate Django with LDAP. I would like to achieve every new Django user to be synchronized with LDAP database which means every new user registers must be populated in LDAP too

OR

I can have LDAP as “Primary database and authentication” for Django where new users are directly created in LDAP.

I tried django-ldap package, unfortunately it can fetch from LDAP but cant do the other way around, so I dropped. The only other option was django-ldapdb package, but I got stuck when running DB Migrations. I am not a pro-developer and might need certain handbooks/step-bystep instructions to resolve my issue, please kindly help me. Below is the error:

I have used these examples from django-ldapdb/examples at master · django-ldapdb/django-ldapdb · GitHub but when I run python manage.py migrate, getting below errors:

(env) root@cdb:/home/cdb# python manage.py makemigrations
Migrations for ‘cdbdb’:

cdbdb/migrations/0005_concretegroup_foogroup_ldapgroup_ldapmultipkroom.py
+ Create model ConcreteGroup
+ Create model FooGroup
+ Create model LdapGroup
+ Create model LdapMultiPKRoom
(env) root@cdb:/home/cdb# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, pntdb, sessions
Running migrations:
Applying cdbdb.0001_initial…Traceback (most recent call last):
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/utils.py”, line 103, in _execute
return self.cursor.execute(sql)
^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py”, line 356, in execute
return super().execute(query)
^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: table “cdbdb_ldapuser” has more than one primary key

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/home/cdb/manage.py”, line 22, in
main()
File “/home/cdb/manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “/home/cdb/env/lib/python3.12/site-packages/django/core/management/init.py”, line 442, in execute_from_command_line
utility.execute()
File “/home/cdb/env/lib/python3.12/site-packages/django/core/management/init.py”, line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/home/cdb/env/lib/python3.12/site-packages/django/core/management/base.py”, line 416, in run_from_argv
self.execute(*args, **cmd_options)
File “/home/cdb/env/lib/python3.12/site-packages/django/core/management/base.py”, line 460, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/core/management/base.py”, line 107, in wrapper
res = handle_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/core/management/commands/migrate.py”, line 353, in handle
post_migrate_state = executor.migrate(
^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/migrations/executor.py”, line 135, in migrate
state = self._migrate_all_forwards(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/migrations/executor.py”, line 167, in _migrate_all_forwards
state = self.apply_migration(
^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/migrations/executor.py”, line 255, in apply_migration
state = migration.apply(state, schema_editor)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/migrations/migration.py”, line 132, in apply
operation.database_forwards(
File “/home/cdb/env/lib/python3.12/site-packages/django/db/migrations/operations/models.py”, line 97, in database_forwards
schema_editor.create_model(model)
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/base/schema.py”, line 512, in create_model
self.execute(sql, params or None)
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/base/schema.py”, line 204, in execute
cursor.execute(sql, params)
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/utils.py”, line 122, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/utils.py”, line 79, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/utils.py”, line 92, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/utils.py”, line 100, in _execute
with self.db.wrap_database_errors:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/utils.py”, line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/utils.py”, line 103, in _execute
return self.cursor.execute(sql)
^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/cdb/env/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py”, line 356, in execute
return super().execute(query)
^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: table “cdbdb_ldapuser” has more than one primary key

Django Version: 5.2.3
Python version: 3.12.7

This can be done by modifying the user-registration process such that when the User object is created you create a new user in the LDAP database.

However, I’ll point out that for this to work you would need to either know where in the schema the new user is to be placed, or else allow for the new schema location to be specified in your new User creation process.

This would require both an LDAP authentication backend and LDAP middleware to be used. You can either find a package that does this or write your own.

My opinion is that I think its going to be highly unlikely that you will find such documentation, because there is no “fixed” pattern for implementation of this. There are an unlimited number of ways of building an LDAP schema between defining your OUs and the data field names you use.

So this raises the question of “Why?” Why are you trying to “Integrate Django with LDAP”? What benefits are you looking to achieve, or what issue are you hoping to address?