Models API Reference

Model Classes

class ldaporm.models.Model(*args, **kwargs)[source]

Bases: object

Base class for LDAP ORM models.

This class provides the core functionality for LDAP-backed models, including field handling, data conversion, validation, and CRUD operations. It mimics Django’s Model class interface to ensure compatibility with Django forms and other Django ORM features.

exception DoesNotExist[source]

Bases: Exception

Raised when a model instance is not found in LDAP.

exception InvalidField[source]

Bases: Exception

Raised when an invalid field is referenced.

exception MultipleObjectsReturned[source]

Bases: Exception

Raised when a query returns more than one object when only one was expected.

__eq__(other: object) bool[source]

Compare this model instance with another.

Equal means:

  • The same class

  • The same DN

  • The same primary key value

Parameters:

other – The object to compare with.

Returns:

True if the objects are equal, False otherwise.

__hash__() int[source]

Return a hash value for this model instance.

Returns:

The hash of the DN.

__init__(*args, **kwargs) None[source]

Initialize a new model instance.

Parameters:
  • *args – Positional arguments for field values.

  • **kwargs – Keyword arguments for field values and special attributes.

Raises:
  • IndexError – If the number of positional arguments exceeds the number of fields.

  • TypeError – If an invalid keyword argument is provided.

__repr__() str[source]

Return a string representation of the model instance.

Returns:

A string representation including the class name.

__str__() str[source]

Return a string representation of the model instance.

Returns:

A string representation including the class name and DN.

clean() None[source]

Hook for doing any extra model-wide validation after we’ve cleaned field via clean_fields. Any ValidationError raised by this method will not be associated with a particular field; it will have a special-case association with the field defined by NON_FIELD_ERRORS.

clean_fields(exclude: list[str] | None = None) None[source]

Clean and validate individual fields.

Parameters:

exclude – List of field names to exclude from validation.

Raises:

ValidationError – If field validation fails.

delete() None[source]

Delete the model instance from LDAP.

classmethod from_db(attributes: list[str], objects: tuple[str, dict[str, list[bytes]]] | list[tuple[str, dict[str, list[bytes]]]], many: bool = False) Model | list[ldaporm.models.Model][source]

Create model instances from raw LDAP data.

Parameters:
  • attributes – List of LDAP attribute names to process.

  • objects – Raw LDAP data objects (dn, attrs) tuples.

  • many – Whether to return multiple objects or a single object.

Returns:

A single model instance or sequence of model instances.

Raises:
  • RuntimeError – If many=False but multiple objects are provided.

  • FieldDoesNotExist – If an LDAP attribute has no corresponding model field.

full_clean(exclude: list[str] | None = None, validate_unique: bool = True) None[source]

Perform full validation on the model instance.

Parameters:
  • exclude – List of field names to exclude from validation.

  • validate_unique – Whether to validate uniqueness (unused, kept for Django compatibility).

Raises:

ValidationError – If validation fails.

classmethod get_password_hash(password: str) bytes[source]

Generate an SSHA password hash for LDAP.

Parameters:

password – The password to hash.

Returns:

The SSHA hash as bytes.

save(commit: bool = True) None[source]

Save the model instance to LDAP.

Parameters:

commit – Whether to commit the changes (unused, kept for Django compatibility).

to_db() tuple[str, dict[str, list[bytes]]][source]

Convert the model instance to LDAP data format.

Returns a 2-tuple similar to what we would get from python-ldap’s ldap.ldapobject.SimpleLDAPObject.search_s:

(DN, {'attr1': ['value'], 'attr2': ['value2'], ...})

This data structure differs from python-ldap in that we don’t prune attributes that have no value attached to them. Those attributes will have value [].

We do this so that when ldaporm.managers.Modlist.modify gets called, it can determine easily which attributes need to be deleted from the object in LDAP.

Returns:

A tuple of (dn, attrs) representing the model in LDAP format.

validate_unique(exclude: list[str] | None = None) None[source]

Validate that the model instance is unique.

Parameters:

exclude – List of field names to exclude from uniqueness validation.

Note

This method is a placeholder for Django compatibility and does not perform actual uniqueness validation.

property dn: str | None

Get the distinguished name (DN) for this model instance.

Returns:

The DN string, or None if not set.

objects: LdapManager | None = None
property pk: Any

The primary key property for this model instance.

class ldaporm.models.LdapModelBase(name, bases, attrs, **kwargs)[source]

Bases: type

Metaclass for LDAP ORM models.

This metaclass handles the creation of LDAP model classes, including field initialization, manager setup, and meta class configuration. It simulates Django’s model creation process to ensure compatibility with Django forms and other Django ORM features.

static __new__(cls, name, bases, attrs, **kwargs)[source]

Create a new LDAP model class.

Parameters:
  • name – The name of the class being created.

  • bases – Base classes for the new class.

  • attrs – Attributes and methods for the new class.

  • **kwargs – Additional keyword arguments.

Returns:

The newly created model class.

add_to_class(name: str, value: Any) None[source]

Add an attribute to the class, calling contribute_to_class if available.

Parameters:
  • name – The name of the attribute to add.

  • value – The value to assign to the attribute.