Skip to content

Advanced Topics

Deep dives into advanced Aksara features.


Overview

These guides cover advanced patterns and features:

Topic Description
Signals Model lifecycle hooks
Custom Fields Creating custom field types
Generic Relations and Durable Workflows Model-agnostic relations and resumable step execution
Background Tasks PostgreSQL-backed queueing and worker lifecycle
Validation Advanced validation patterns
Caching Query and response caching
Testing Testing patterns and fixtures
Performance Optimization techniques

Prerequisites

These guides assume familiarity with:


Signals

Hook into model lifecycle events:

from aksara import Model, fields
from aksara.signals import pre_save, post_save

class Post(Model):
    title = fields.String(max_length=200)
    slug = fields.String(max_length=200)

@pre_save(Post)
async def generate_slug(sender, instance, **kwargs):
    if not instance.slug:
        instance.slug = slugify(instance.title)

Learn about Signals

Custom Fields

Create specialized field types:

from aksara.fields import Field

class PhoneField(Field):
    def __init__(self, region="US", **kwargs):
        self.region = region
        super().__init__(**kwargs)

    def validate(self, value):
        # Custom validation
        pass

Create Custom Fields

Validation

Complex validation patterns:

from aksara.validation import validator, ValidationError

class Order(Model):
    @validator("quantity")
    def validate_quantity(cls, v):
        if v <= 0:
            raise ValidationError("Must be positive")
        return v

Advanced Validation

Caching

Speed up your application:

from aksara.cache import cached, cache

# Cache query results
@cached(ttl=300)
async def get_popular_posts():
    return await Post.objects.filter(is_popular=True).all()

Caching Guide

Testing

Comprehensive testing:

from aksara.testing import AksaraTestCase, factory

class UserFactory(factory.Factory):
    class Meta:
        model = User

    email = factory.Faker("email")
    name = factory.Faker("name")

Testing Patterns

Performance

Optimize your application:

# N+1 prevention
posts = await Post.objects.select_related("author").prefetch_related("comments").all()

# Query profiling
from aksara.debug import profile_queries

@profile_queries
async def my_view():
    # Queries are logged and analyzed
    pass

Performance Guide