Custom Fields System
Ralph’s custom fields system allows you to add dynamic fields to models without modifying the database schema. This is useful for organization-specific data that doesn’t fit into the core model structure.Overview
Custom fields are:- Dynamic: Created through the admin interface, no code changes required
- Typed: Support string, integer, date, URL, and choice list types
- Inheritable: Can be inherited from related objects
- API-enabled: Automatically exposed in the REST API
- Permission-controlled: Can be restricted to specific user groups
Adding Custom Fields to Models
Step 1: Add the Mixin
MixWithCustomFieldsMixin into your model:
src/ralph/lib/custom_fields/models.py:212-221
Step 2: Enable Admin Integration
MixCustomFieldValueAdminMixin into your admin class:
src/ralph/lib/custom_fields/admin.py:67-84
Step 3: Enable API Integration
For Django Rest Framework, mixWithCustomFieldsSerializerMixin into your serializer:
docs/development/custom-fields.md:12-14
Field Types
Ralph supports five custom field types:String
Plain text values:Integer
Numeric values:Date
Date values (YYYY-MM-DD format):URL
Validated URL values:Choice List
Predefined choices separated by pipe (|):src/ralph/lib/custom_fields/models.py:23-48
Creating Custom Fields
Custom fields are created through the admin interface at/admin/custom_fields/customfield/:
- Navigate to Custom Fields in the admin
- Click Add Custom Field
- Fill in the details:
- Name: Display name (e.g., “Purchase Order Number”)
- Attribute Name: Auto-generated slug (e.g., “purchase_order_number”)
- Type: Select from String, Integer, Date, URL, or Choice List
- Choices: For Choice List type, enter options separated by
| - Default Value: Optional default value
- Managing Group: Restrict editing to specific group (optional)
- Use as Configuration Variable: Expose in API configuration_variables
Using Custom Fields
In Python Code
Access custom field values:src/ralph/lib/custom_fields/models.py:222-242
Direct QuerySet Access
src/ralph/lib/custom_fields/models.py:125-149
In Templates
Display custom fields in admin templates:Custom Field Inheritance
Custom fields can be inherited from related objects. For example, a virtual server can inherit custom fields from its hypervisor:virtual_server.custom_fields.all(), it returns both:
- Custom fields set directly on the virtual server
- Custom fields inherited from its hypervisor
src/ralph/lib/custom_fields/models.py:190-210
Clearing Inherited Values
When a parent object’s custom field is deleted, you can clear it from child objects:src/ralph/lib/custom_fields/models.py:244-265
Permission Management
Restrict custom field editing to specific groups:- Only users in that group can create/edit/delete values for this field
- Other users can still view the field values
- Useful for sensitive data like license keys or compliance information
src/ralph/lib/custom_fields/models.py:75-84
Configuration Variables
Mark custom fields as configuration variables to expose them in the API:configuration_variables:
src/ralph/lib/custom_fields/models.py:86-93, src/ralph/lib/custom_fields/models.py:229-236
Admin Interface Features
Custom Field Values Summary
The admin shows a summary of all custom field values, including inherited ones:- Field name
- Current value
- Source object (if inherited)
- Link to source object
src/ralph/lib/custom_fields/admin.py:67-133
Inline Editing
Custom fields appear as an inline formset in the change form, allowing you to:- Add new custom field values
- Edit existing values
- Delete custom field values
- Clear inherited values (if applicable)
src/ralph/lib/custom_fields/admin.py:55-65
Data Model
CustomField Model
Defines the custom field configuration:src/ralph/lib/custom_fields/models.py:50-95
CustomFieldValue Model
Stores the actual values:src/ralph/lib/custom_fields/models.py:125-149
Best Practices
- Use descriptive names: “Purchase Order Number” instead of “PO Num”
- Choose appropriate types: Use Integer for numbers, Date for dates, etc.
- Set defaults wisely: Provide sensible defaults to reduce data entry
- Use managing groups: Protect sensitive fields from unauthorized edits
- Mark config variables: Only mark fields as configuration variables if they’re needed by external tools
- Avoid overuse: Don’t replace proper model fields with custom fields for core functionality
Complete Example
Here’s a complete example of adding custom fields to an Asset model:- Create custom fields in the admin at
/admin/custom_fields/customfield/ - Add custom field values when editing assets
- Access values via
asset.custom_fields_as_dictin Python - See custom fields in API responses automatically
Next Steps
- Learn about RalphAdmin customization for more admin features
- Explore Transitions for workflow management
- Check out the API documentation for API integration