DefaultHeadlessAdapter
TheDefaultHeadlessAdapter class provides default implementations for all adapter methods. You can override specific methods to customize behavior.
Location: allauth.headless.adapter.DefaultHeadlessAdapter
Configuration
Specify your custom adapter in Django settings:Creating a Custom Adapter
ExtendDefaultHeadlessAdapter and override the methods you need:
Adapter Methods
serialize_user
Serialize user data for API responses.User
Django user instance to serialize
dict
Dictionary containing user data
get_user_dataclass() and user_as_dataclass() instead.
get_user_dataclass
Return a dataclass representing the user schema.type
A Python dataclass type
id: User primary key (int, str, or UUID)display: Display name (str)email: Primary email address (Optional[str])username: Username (str, ifUSER_MODEL_USERNAME_FIELDis set)has_usable_password: Whether password is set (bool)
user_as_dataclass
Convert a user instance to the dataclass returned byget_user_dataclass().
User
Django user instance
dataclass
Instance of the dataclass from
get_user_dataclass()get_frontend_url
Return the frontend URL for a given URL name.string
URL name (e.g.,
account_email_verification_sent, account_reset_password)dict
URL parameters (e.g.,
key for verification links)string
Frontend URL
HEADLESS_FRONTEND_URLS setting:
error_messages
Dictionary of error messages.Accessing the Adapter
You can access the configured adapter instance usingget_adapter():
Common Customization Patterns
Adding Profile Data
Include related profile data in user serialization:Including Permissions
Add user permissions to the serialized data:Multi-tenant Support
Add tenant information:Custom Display Name
Customize how display names are generated:Localized Error Messages
Provide translated error messages:Dynamic Frontend URLs
Generate frontend URLs based on request context:Integration with OpenAPI Specification
When you customizeget_user_dataclass(), the OpenAPI specification is automatically updated to reflect your custom schema.
Example: After defining a custom dataclass with additional fields:
Testing Custom Adapters
Test your custom adapter methods:Best Practices
- Keep it simple: Only override methods you need to customize
- Call super(): Use
super()to preserve default behavior when extending methods - Avoid queries: Don’t make database queries in
serialize_user()if possible; useselect_related()orprefetch_related()before calling - Use dataclasses: For OpenAPI integration, use
get_user_dataclass()instead of overridingserialize_user() - Test thoroughly: Write tests for all custom adapter methods
- Document changes: Document any custom behavior for your team
- Consider performance: Be mindful of performance when adding fields that require additional queries
Related Settings
string
default:"allauth.headless.adapter.DefaultHeadlessAdapter"
Path to custom adapter class
dict
default:"{}"
Mapping of URL names to frontend URL patterns
