Group-Based Features
Group-based feature toggles allow developers to show or hide features based on a user's membership in Django groups. This system is used for alpha/beta testing, gradual rollouts, and access control.
Implementation
The feature toggle system uses a custom Django template filter that checks group membership.
Template Tag
The in_group filter is defined in core/templatetags/group_tags.py:
@register.filter
def in_group(user, group_name):
"""Check if a user is in a specific group by name."""
if not user or not user.is_authenticated:
return False
try:
group = Group.objects.get(name=group_name)
return user.groups.filter(pk=group.pk).exists()
except Group.DoesNotExist:
return FalseUsage in Templates
Load the template tag library:
Check group membership:
Examples
Navigation Items
Feature Sections
Conditional Elements
Managing Groups
Creating Groups
Via Django Admin:
Navigate to
/admin/auth/group/Click "Add Group"
Enter group name
Save
Via Django Shell:
Via Data Migration:
Adding Users to Groups
Via Django Admin:
Navigate to user's detail page
Select groups in "Groups" field
Save
Via Django Shell:
Testing
Test group membership in unit tests:
Current Feature Groups
Campaigns Alpha
Early access to campaign features
Campaign navigation link, campaign creation
Best Practices
Descriptive Names: Use clear group names like "Campaigns Alpha", "Beta Testers"
Document Groups: Maintain a list of active groups and their purposes
Fail Safe: The filter returns
Falsefor non-existent groupsClean Up: Remove groups after features are fully released
Test Coverage: Include tests for both group members and non-members
Security Considerations
Group membership is checked server-side
Template filter prevents rendering, not just hiding with CSS
Groups should not be used for critical security permissions
Use Django's permission system for access control
Performance
The in_group filter:
Makes one database query per check
Results are not cached by default
Consider using
select_related('groups')for user queries if checking many groups
Last updated