Skip to main content

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

Mix WithCustomFieldsMixin into your model:
Source: src/ralph/lib/custom_fields/models.py:212-221

Step 2: Enable Admin Integration

Mix CustomFieldValueAdminMixin into your admin class:
Source: src/ralph/lib/custom_fields/admin.py:67-84

Step 3: Enable API Integration

For Django Rest Framework, mix WithCustomFieldsSerializerMixin into your serializer:
Source: 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 (|):
Source: 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/:
  1. Navigate to Custom Fields in the admin
  2. Click Add Custom Field
  3. 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:
Source: src/ralph/lib/custom_fields/models.py:222-242

Direct QuerySet Access

Source: 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:
Now when you access virtual_server.custom_fields.all(), it returns both:
  • Custom fields set directly on the virtual server
  • Custom fields inherited from its hypervisor
Source: 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:
Source: src/ralph/lib/custom_fields/models.py:244-265

Permission Management

Restrict custom field editing to specific groups:
When a managing group is set:
  • 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
Source: src/ralph/lib/custom_fields/models.py:75-84

Configuration Variables

Mark custom fields as configuration variables to expose them in the API:
These fields are then available in API responses under configuration_variables:
This is useful for configuration management tools like Ansible or Puppet. Source: 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:
The summary displays:
  • Field name
  • Current value
  • Source object (if inherited)
  • Link to source object
Source: 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)
Source: src/ralph/lib/custom_fields/admin.py:55-65

Data Model

CustomField Model

Defines the custom field configuration:
Source: src/ralph/lib/custom_fields/models.py:50-95

CustomFieldValue Model

Stores the actual values:
Source: src/ralph/lib/custom_fields/models.py:125-149

Best Practices

  1. Use descriptive names: “Purchase Order Number” instead of “PO Num”
  2. Choose appropriate types: Use Integer for numbers, Date for dates, etc.
  3. Set defaults wisely: Provide sensible defaults to reduce data entry
  4. Use managing groups: Protect sensitive fields from unauthorized edits
  5. Mark config variables: Only mark fields as configuration variables if they’re needed by external tools
  6. 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:
Now you can:
  1. Create custom fields in the admin at /admin/custom_fields/customfield/
  2. Add custom field values when editing assets
  3. Access values via asset.custom_fields_as_dict in Python
  4. See custom fields in API responses automatically

Next Steps