The Prefill Sources feature allows you to configure reusable data sources that automatically populate form fields with data from various sources including:
- User Model - Current user's profile data
- LDAP/Active Directory - Enterprise directory attributes
- External Databases - Custom database queries with flexible field mappings
- APIs - External API calls
- System Values - Current date/time, previous submissions
This makes the library flexible for different deployment scenarios while providing a user-friendly dropdown interface for form builders.
- Reusable Configuration - Define a prefill source once, use it across multiple forms
- Flexible Field Mapping - Customize database lookup fields for different environments
- User-Friendly - Form builders select from a dropdown instead of typing complex syntax
- Centralized Management - All prefill sources managed in Django Admin
- Backward Compatible - Legacy text-based prefill sources still work
Run the management command to create standard prefill sources:
python manage.py seed_prefill_sourcesThis creates sources for:
- Current User fields (email, first name, last name, username)
- LDAP attributes (department, title, manager, phone, employee ID)
- System values (current date, current datetime, last submission)
For database lookups, create custom prefill sources via Django Admin:
Admin → Prefill Sources → Add Prefill Source
Name: Employee - First Name
Source Type: Database
Source Key: {{ dbo.EMPLOYEES.FIRST_NAME }}
Description: Employee's first name from external HR database
Database Configuration:
- DB Alias: hr_database
- DB Schema: dbo
- DB Table: EMPLOYEES
- DB Column: FIRST_NAME
- DB Lookup Field: EMPLOYEE_ID
- DB User Field: employee_id
Name: Employee - Department
Source Type: Database
Source Key: {{ hr.employees.department }}
Description: Employee department from HR database
Database Configuration:
- DB Alias: hr_database
- DB Schema: hr
- DB Table: employees
- DB Column: department
- DB Lookup Field: email
- DB User Field: email
When creating or editing form fields in Django Admin:
- Go to Form Definitions → Select a form → Edit
- In the form field inline, expand "Choices & Defaults"
- Select a prefill source from the "Prefill source config" dropdown
- Save the form
The field will now automatically populate with data from the selected source when users open the form.
When configuring database prefill sources, you need to specify how to match the current user to a database record:
- DB Lookup Field - The column in the external database to match against (e.g.,
ID_NUMBER,EMAIL,EMPLOYEE_ID) - DB User Field - The UserProfile field containing the value to match (e.g.,
employee_id,email,external_id)
Your external database has an ID_NUMBER column, and your UserProfile has an employee_id field:
DB Lookup Field: ID_NUMBER
DB User Field: employee_id
Generated SQL:
SELECT [FIRST_NAME]
FROM [dbo].[STBIOS]
WHERE [ID_NUMBER] = %s -- User's employee_idYour external database has an EMAIL column, and you want to match by the user's email:
DB Lookup Field: EMAIL
DB User Field: email
Generated SQL:
SELECT [department]
FROM [hr].[employees]
WHERE [EMAIL] = %s -- User's emailYour external database has a EXTERNAL_USER_ID column, and your UserProfile has an external_id field:
DB Lookup Field: EXTERNAL_USER_ID
DB User Field: external_id
Configure database connections in your Django settings:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'hr_database': {
'ENGINE': 'mssql',
'NAME': 'HRDatabase',
'USER': 'readonly_user',
'PASSWORD': 'password',
'HOST': 'sql.example.com',
'PORT': '1433',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
},
},
}
# Optional: Configure default database source settings
FORMS_WORKFLOWS = {
'DATABASE_SOURCE': {
'database_alias': 'hr_database',
'default_schema': 'dbo',
'user_id_field': 'employee_id',
'lookup_field': 'EMPLOYEE_ID',
}
}Admin → Prefill Sources
- List View - See all prefill sources, filter by type, toggle active status
- Edit View - Configure source details and type-specific settings
- Order - Control the display order in dropdowns
Admin → Form Definitions → [Select Form] → Form Fields
Each form field has a "Prefill source config" dropdown showing all active prefill sources grouped by type.
If you have existing forms using the legacy text-based prefill_source field:
- The old field still works (backward compatible)
- Create matching PrefillSource records
- Update form fields to use
prefill_source_config - The new field takes precedence over the legacy field
The included farm demo showcases prefill functionality:
python manage.py seed_farm_demoThis creates a "Farmer Contact Update" form with fields pre-filled from:
- User's first name
- User's last name
- User's email
- Current date
Try it out:
- Login as any demo user (password:
farm123) - Navigate to "Farmer Contact Update" form
- See fields automatically populated with your user data
name- Display name shown in dropdownsource_type- Type of source (user, ldap, database, api, system, custom)source_key- Source identifier stringdescription- Help text for form buildersis_active- Whether source appears in dropdownsorder- Display order in dropdowns
db_alias- Django database aliasdb_schema- Database schema namedb_table- Table namedb_column- Column to retrievedb_lookup_field- Column to match againstdb_user_field- UserProfile field for matching
ldap_attribute- LDAP attribute name
api_endpoint- API URLapi_field- Field to extract from response
get_prefill_source_key()- Returns the prefill source identifier, prioritizingprefill_source_configover legacyprefill_source
- Check UserProfile - Ensure the user has the required field populated (e.g.,
employee_id) - Check Database Connection - Verify the database alias is configured in
DATABASES - Check Permissions - Ensure the database user has SELECT permissions
- Check Logs - Look for error messages in Django logs
- Test Query - Try the SQL query manually with a known user ID
"No data found for user"
- The user's profile field is empty or doesn't match any database records
- Check the
db_user_fieldanddb_lookup_fieldconfiguration
"Database data source is not available"
- The database alias is not configured in
DATABASES - Add the database configuration to settings.py
"Invalid identifier"
- SQL injection protection rejected the field name
- Ensure field names contain only letters, numbers, and underscores
- Use Descriptive Names - Make prefill source names clear for form builders
- Group by Type - Use consistent naming (e.g., "Student - First Name", "Student - Last Name")
- Document Custom Sources - Add descriptions explaining what each source does
- Test Before Deploying - Verify prefill sources work with test users
- Secure Database Access - Use read-only database users for external databases
- Monitor Performance - Database prefills add queries; consider caching for high-traffic forms
- Explore the Configuration Guide for more settings
- Read the Quickstart Guide to create your first form
- Check out the Value Proposition to learn more about the library