django-organizations
repository·master·Indexed 23 days ago
https://github.com/bennylope/django-organizationsA tool for managing user-managed, multi-user groups (organizations) in Django. It separates individual user identity from group-based accounts, subscriptions, and permissions. The library supports various implementation patterns including proxy models for custom naming, multi-table inheritance for adding custom fields, and abstract or base models for fully customized organization systems.
What's inside django-organizations
- django-organizations is a Django application designed to provide group account functionality. It allows you to consolidate user access and rights into group accounts, enabling multi-user account management within your Django project.
Extend invitation and registration backends
masterThe invitation and registration backends allow account users to add new users to their accounts and enable user self-registration.
Each base backend class provides a common interface designed to work with your specific user models, registration systems, or other account tools. The default invitation backend accepts an email address and returns a user that either matches that email or creates a new user with that email. The view responsible for adding the user is then tasked with linking that user to the organization.
Use django-organizations views and mixins
masterThe project provides default class-based views for managing organizations and memberships. These views are built on a set of mixins that:
- Limit access based on the user's relationship to an organization.
- Automatically query the correct organization or user based on URL keywords.
Developers are encouraged to use these mixins as a foundation for building custom, project-specific functionality.
Understand the OrganizationUser model
masterTheOrganizationUsermodel represents a membership link between a User and anOrganization. It is used to manage the relationship and potentially store metadata or roles specific to that user's membership within a particular organization.Use custom models for organizations
masterInstead of installing the app as a standard Django app, you can usedjango-organizationsas a base library to build your own unique organization model sets using custom database tables. This is useful for advanced customization where the default 'concrete' models do not meet your requirements.Understand the OrganizationOwner model
masterTheOrganizationOwnermodel identifies users who have ownership privileges within anOrganization. Owners typically have elevated permissions to manage the organization's settings and membership.Core Concepts: Organization, OrganizationUser, and OrganizationOwner
masterThe library provides three primary models to manage multi-user groups:
- Organization: The central group object. This is the entity you associate with your application's features (e.g., subscriptions, projects, or repositories).
- OrganizationUser: A custom
throughmodel for the ManyToMany relationship betweenOrganizationandUser. It stores user-specific metadata for that organization and facilitates ownership links. - OrganizationOwner: A model representing the user with authority over the organization's lifecycle. It maintains a one-to-one relationship with an
OrganizationUser, allowing a singleUserto own multiple organizations.
Understand the core organization models
masterThe library is built around three primary models:
- Organization: The group object (e.g., a company, a project, or a subscription container).
- Organization User: A
throughmodel relating a User to an Organization. It allows storing organization-specific user data and facilitates ownership. - Organization Owner: A model linking to an Organization User who has administrative rights over the organization.
Relationship Model:
- An
OrganizationUseris associated with exactly oneOrganization. - A
Usercan be associated with multipleOrganizationUsers, allowing a single user to belong to multipleOrganizations.
Note: Because of this many-to-many relationship, do not use the
OrganizationUserclass as aUserProfile(which requires a one-to-one relationship). Use a dedicated profile model for user-specific data instead.Understand the Organization model
masterTheOrganizationmodel is the central entity indjango-organizations. It represents a distinct group or entity that users can belong to. It serves as the primary container for managing memberships and permissions within the package.Compare abstract vs base models
masterWhen choosing how to implement your organization models, consider the following differences:
Feature Abstract Models ( organizations.abstract)Base Models ( organizations.base)Included Fields Timestamps, slug field, and is_adminon the user model.Bare minimum fields only. Customization Uses pre-implemented logic for slugs and timestamps. You must manually add slugs, timestamps, or any other fields. Use Case Best if you are happy with the default implementation of common fields. Best for highly custom implementations where you want total control. Implement hierarchical organizations with simple inheritance
masterYou can create relationships between different types of organizations by using inheritance between your custom organization models. This allows one organization type to act as a parent to another.
Example of an
Associationcontaining multipleTeams:from django.db import models from organizations.models import Organization from sports.models import Sport class Association(Organization): sport = models.ForeignKey(Sport, related_name="associations") class Team(Organization): association = models.ForeignKey(Association, related_name="teams") city = models.CharField(max_length=100)Configure django-organizations default models
masterTo use the default organization, organization user, and organization owner models, add
organizationsto yourINSTALLED_APPSin your Django settings file. Ensure you already have a user system (likedjango.contrib.auth) in place.INSTALLED_APPS = ( 'django.contrib.auth', 'organizations', )