django-organizations

repository·master·Indexed 23 days ago

https://github.com/bennylope/django-organizations

A 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.

Tokens
14.8K
Snippets
30
Records
95
Agent score
78%

What's inside django-organizations

  1. Extend invitation and registration backends

    master

    The 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.

  2. Use django-organizations views and mixins

    master

    The 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.

  3. Use custom models for organizations

    master
    Instead of installing the app as a standard Django app, you can use django-organizations as 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.
  4. Core Concepts: Organization, OrganizationUser, and OrganizationOwner

    master

    The 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 through model for the ManyToMany relationship between Organization and User. 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 single User to own multiple organizations.
  5. Understand the core organization models

    master

    The library is built around three primary models:

    1. Organization: The group object (e.g., a company, a project, or a subscription container).
    2. Organization User: A through model relating a User to an Organization. It allows storing organization-specific user data and facilitates ownership.
    3. Organization Owner: A model linking to an Organization User who has administrative rights over the organization.

    Relationship Model:

    • An OrganizationUser is associated with exactly one Organization.
    • A User can be associated with multiple OrganizationUsers, allowing a single user to belong to multiple Organizations.

    Note: Because of this many-to-many relationship, do not use the OrganizationUser class as a UserProfile (which requires a one-to-one relationship). Use a dedicated profile model for user-specific data instead.

  6. Compare abstract vs base models

    master

    When choosing how to implement your organization models, consider the following differences:

    FeatureAbstract Models (organizations.abstract)Base Models (organizations.base)
    Included FieldsTimestamps, slug field, and is_admin on the user model.Bare minimum fields only.
    CustomizationUses pre-implemented logic for slugs and timestamps.You must manually add slugs, timestamps, or any other fields.
    Use CaseBest if you are happy with the default implementation of common fields.Best for highly custom implementations where you want total control.
  7. Implement hierarchical organizations with simple inheritance

    master

    You 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 Association containing multiple Teams:

    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)