Aggregates

class django_declarative_apis.machinery.attributes.Aggregate(aggregation_function=None, **kwargs)[source]

Bases: EndpointAttribute

DDA uses aggregates to perform memoization to avoid repeated calculations, querying, or any task that can be performed once and the result cached. Aggregates retrieve or create a related object based on one or more field that is in use in the EndpointDefinition. An aggregate is calculated only once and then the data is cached for future retrieval.

Aggregates are used as decorators on functions.

from django_declarative_apis.machinery import aggregate

class SampleClass:
    # code

    @aggregate
    def sample_function():
        # code
Parameters
  • required (optional) – Defines whether the aggregate is required or not. Defaults to False.

  • depends_on (optional) – Reference to another aggregate that should be run before this aggregate. Defaults to None.

Example: We want to query a user only once and cache that information for future use.

from django_declarative_apis.machinery import aggregate

class SampleClass:
    user_id = url_field()

    @aggregate(required=True)
    def get_user(self):
        try:
            user = models.User.objects.get(id=self.user_id)
        except:
            raise Exception("User with matching id not found")
        return user