When you open Settings in Horilla CRM and check the role permissions, you will see models such as Lead, Account, and Opportunity. You will not see internal models that store rule conditions, column preferences, or recently viewed records. These models are excluded to keep the permission screens focused on the models users actually manage.

Horilla CRM provides the @permission_exempt_model decorator for this purpose. If you are developing an extension app, you can use it to keep internal models out of the permission screens without changing the core code.

Why Permission Screens Need Filtering

Django creates four default permissions for each model: add, change, delete, and view. Without filtering, Horilla’s permission screens could list every model in the project, including tables that users never manage directly.

For example:

  • LeadAssignmentCondition stores conditions belonging to an assignment rule.
  • ListColumnVisibility stores a user’s column visibility preferences.
  • RecentlyViewed stores records that a user has recently accessed.

These models support other features rather than representing independent business objects. Exposing their permissions would clutter the interface and could cause administrators to grant unnecessary permissions when using the select-all option.

How @permission_exempt_model Works

The decorator is defined in horilla/registry/permission_registry.py. It registers a model’s class name in the PERMISSION_EXEMPT_MODELS set and returns the class unchanged.

You can apply it directly to a model:

from horilla.registry.permission_registry import permission_exempt_model


@permission_exempt_model
class ContractClause(HorillaCoreModel):

The registry already includes models such as Session, Migration, LogEntry, Group, Permission, ContentType, and Attachment.

Horilla also provides the is_permission_exempt(model) helper to determine whether a model should be excluded. In addition to checking the registered class names, it handles extension classes created using _inherit_model, which extend existing models rather than introducing independent permission entries.

Where the Exclusion Takes Effect

Horilla uses is_permission_exempt() when processing installed models for its permission screens. This affects two areas.

Permission lists

Exempt models are omitted from the role and user permission lists. Administrators will not see their permission checkboxes or find them through the corresponding model search.

Select-all options

The select-all functionality also skips exempt models. Selecting all available permissions therefore does not include permissions for these models through that interface.

What the Decorator Does Not Do

It is important to understand that @permission_exempt_model controls the permission management interface. It does not disable Django’s permission system or automatically grant access to a model.

  • Django still creates the permissions. The decorator does not remove the model’s permissions from Django.
  • View-level checks still apply. A view protected by permission_required_or_denied([“contracts.view_contractclause“]) will still check the specified permission.
  • Access is not inherited automatically. If clauses should follow their contract’s access rules, your application must enforce that relationship explicitly.

For example, if users can manage contract clauses only when they have permission to manage the parent contract, the clause views should check the relevant contract permissions and enforce any necessary object-level access restrictions.

Exempting a model only removes it from the permission management screens. You must still decide how access to its records should be controlled.

When to Use the Decorator

The decorator is useful when a model supports another feature and should not be managed independently through the permission screens.

Suitable use cases

  • Child records: Assignment-rule conditions, scoring-rule conditions, workflow steps, approval conditions, review conditions, and cadence conditions.
  • Personal preferences: Column visibility settings, recently viewed records, active tab preferences, and saved filters.
  • Technical models: Sessions, content types, and other internal records that do not need independent permission management.

When not to use it

Do not exempt models that represent independent business objects users manage directly. For example, models such as Contract and Product should remain visible when administrators need to assign their view, add, change, and delete permissions independently.

A simple rule to follow is this: if administrators need to control access to a model independently, keep it visible. If the model exists only to support another feature and its access is managed through the parent or application logic, consider exempting it.

Step-by-Step Implementation

1. Import the Decorator

Import permission_exempt_model from the permission registry and apply it to the model class.

from horilla.registry.permission_registry import permission_exempt_model




@permission_exempt_model
class ContractClause(HorillaCoreModel):
    contract = models.ForeignKey(
        "contracts.Contract",
        on_delete=models.CASCADE,
    )
    text = models.TextField()

Make sure the imports for HorillaCoreModel and models are also present in your module, using the correct paths for your project.

2. Protect Access Through the Parent

If contract clauses should follow the contract’s access rules, enforce the appropriate permission checks in the views that create, update, delete, or display clauses.

For example, use the relevant contract permissions, such as contracts.change_contract, where appropriate. Also validate access to the specific parent contract rather than relying only on a general model-level permission.

3. Verify the Permission Screens

Start or reload the application as required by your development setup, then open the role or user permission screen.

Confirm that ContractClause no longer appears in the permission list and that the select-all functionality skips it.

The decorator stores the registered names in an in-memory set, so no database migration is required for the decorator itself. However, the model must be imported when the application loads for the registration to take effect.

Things to Keep in Mind

Model names must be unique for reliable filtering. The registry stores cls.__name__, not the combination of app label and model name. If two apps define models with the same class name, registering one name can affect both. Use distinctive model names to avoid unintended exclusions.

The model module must load. Registration happens when the decorated class is imported. Ensure the model belongs to an installed app and is loaded during application startup.

Check existing decorators before assuming a model is exempt. Some decorators in the codebase may be commented out. A model is not registered through a commented-out decorator.

Use the correct decorator name. The decorator’s docstring may contain an outdated example using @exclude_no_perm. The current name is @permission_exempt_model.

Quick Summary

QuestionAnswer
What does it do?Excludes a model from the role and user permission screens and their select-all options.
Does it remove Django permissions?No. Django continues to create them.
Does it grant or inherit access?No. Access must be enforced separately.
When should it be used?For internal child records, personal preferences, and technical models.
When should it be avoided?When administrators need to manage the model’s permissions independently.
Is a migration required?No migration is required for the decorator itself.

The @permission_exempt_model decorator helps keep Horilla’s permission screens clear by excluding models that do not need independent permission management. It is particularly useful for internal records, user preferences, and child models that support larger features.

When using it in an extension app, make sure access is still enforced through the appropriate views and parent-model permissions. The decorator simplifies permission management, but it does not replace access-control checks.

Before publishing, verify the Settings menu path, test the permission list and select-all behavior, and capture a screenshot. Also confirm that HorillaCoreModel is imported from horilla.contrib.core.models in the version being documented.

Share this article