Skip to main content
Ralph’s API is built on Django REST Framework and provides a comprehensive REST API for all core models. The API architecture follows consistent patterns and includes built-in features for permissions, filtering, and serialization.

Quick Start

Create a new API resource in three simple steps:
See src/ralph/api/__init__.py:1 for available base classes.

Core Components

RalphAPISerializer

The base serializer class provides comprehensive features for Ralph models. Built-in Features:
  • Automatic URL and PK fields: Both object primary key and URL are included automatically (see ralph.api.serializers.RalphAPISerializer.get_default_field_names:34 for implementation)
  • Context propagation: Every sub-serializer has the current context set, even when instantiated directly as a field
  • Reversed choice fields: All choice fields use ReversedChoiceField, allowing values by name instead of key. Works seamlessly with dj.choices.Choices
  • Field-level permissions: Uses PermissionsPerFieldSerializerMixin to return only fields the user has access to. View-only permissions are handled as read-only fields
  • Object-level permissions for relations: Related field models with object-level permissions only show accessible objects
  • Model validation: Data is validated using the model’s clean() method. Disable with _validate_using_model_clean = False in your serializer
Example with Advanced Features:
See src/ralph/virtual/api.py:119 for a real implementation.

RalphAPISaveSerializer

For write operations (POST, PUT, PATCH), use RalphAPISaveSerializer which simplifies relation handling:
See src/ralph/virtual/api.py:84 for the complete example.

RalphAPIViewSet

The base viewset class handles permissions, filtering, and query optimization. Built-in Features:
  • Automatic save serializer: Default serializer for POST/PUT/PATCH uses PrimaryKeyRelatedField for all relations. Override with save_serializer_class attribute
  • Query optimization: Includes QuerysetRelatedMixin for easy select_related and prefetch_related. Defaults to admin site’s list_select_related
  • Admin integration: Through AdminSearchFieldsMixin, search fields are automatically taken from the related admin site
  • Comprehensive permissions: Includes RalphPermission checking:
    • User authentication (IsAuthenticated)
    • Object-level permissions (ObjectPermissionsMixin)
    • Django admin permissions (add_*, change_*, delete_*)
Example ViewSet:
See src/ralph/virtual/api.py:249 for the complete implementation.

Filter Backends

RalphAPIViewSet includes powerful filtering capabilities through multiple filter backends:

DjangoFilterBackend

Provides basic filtering for Django models. Filter fields are automatically associated with the model’s admin configuration.

PermissionsForObjectFilter

Limits results to objects the user has at least view permission for.

ExtendedFiltersBackend

Allows filtering multiple fields using a single query parameter. Especially useful for polymorphic models:
Query: GET /api/cloud-hosts/?name=abc resolves to:
See src/ralph/virtual/api.py:277 for an example.

LookupFilterBackend

Filter by Django lookups using __ convention:

TagsFilterBackend

Filter queryset by tags. Multiple tags can be specified:

OrderingFilter

Order queryset using the ordering URL parameter:

Polymorphic Models

Polymorphic models automatically support lookup filters and extended filters:
This query checks all fields defined in extended_filter_fields (asset__hostname, ip__address, cloudhost__hostname) to see if any start with “abc”.

Router Registration

All viewsets must be registered with Ralph’s router:
See src/ralph/virtual/api.py:353 for registration examples.

Advanced Patterns

Custom Destroy Logic

Implement custom deletion behavior with force delete option:
See src/ralph/virtual/api.py:200 for the implementation.

Query Annotations

Add computed fields to querysets:
See src/ralph/virtual/api.py:343 for the complete example.

Best Practices

  1. Always use Ralph base classes: Use RalphAPISerializer and RalphAPIViewSet for consistency and built-in features
  2. Optimize queries: Define select_related and prefetch_related to avoid N+1 queries
  3. Separate read/write serializers: Use serializer_class for reads and save_serializer_class for writes
  4. Leverage extended filters: For polymorphic models, define extended_filter_fields to enable unified searching
  5. Test permissions: Ensure field-level and object-level permissions are working as expected
  6. Document custom endpoints: Add docstrings to custom actions and methods