Computed Properties

A @computed property is a derived value: a method computed from your state and read in the template like a plain attribute. Within a request it runs once and the result is reused, so it is cheaper than a plain @property referenced several times, and the value is never stored or sent as state.

Basic usage

full_name and initials are @computed from first/last - they update live as you type, and aren't stored or sent as state.

PREVIEW

Badge

AL
Ada Lovelace
class ComputedDemo(Component):
    first: str = "Ada"
    last: str = "Lovelace"

    @computed
    def full_name(self):
        return f"{self.first} {self.last}".strip()

    @computed
    def initials(self):
        return "".join(part[0] for part in [self.first, self.last] if part).upper()

    def inline_template(self):
        return """
        <div class="...">
            <div class="...">
                <input lace:model.live="first" class="...">
                <input lace:model.live="last" class="...">
            </div>
            <p class="uppercase">Badge</p>
            <div class="flex items-center gap-3">
                <div class="avatar-circle">{{ initials }}</div>
                <span>{{ full_name }}</span>
            </div>
        </div>
        """

Within a single request a @computed runs once and its result is reused, so referencing full_name several times in a template is one call, not one per use.

@computed vs @property

Feature @property @computed
Within-request caching No Yes
Cross-request caching No Optional
Global caching No Optional
Use case Simple derivations Expensive calculations, DB queries

Caching across requests

By default a @computed is recomputed on every request (cached only within that request). When the work is expensive, you can cache the result across requests, per component instance or globally with Django's cache backend. See Caching.

View this page as markdown - handy as context for your AI tools. Full index at /llms.txt.

Theme
Accent
Ink