Skip to content

Model Reference

Django ActivityPub Toolkit uses several categories of models to represent ActivityPub data structures, federation state, and application-specific entities.

Core Federation Models

These models manage the fundamental entities for ActivityPub federation.

Domains and References

activitypub.core.models.Domain

Bases: TimeStampedModel

activitypub.core.models.Reference

Bases: TimeStampedModel, StatusModel

The Reference is the base class for any JSON-LD context.

activitypub.core.models.LinkedDataDocument

Bases: Model

A linked data document contains only the source JSON-LD documents

get_graph(data) staticmethod

Parse JSON-LD data into an RDF graph. Returns the raw graph without any modifications.

sanitize_graph(g, domain) staticmethod

Sanitize an RDF graph by: 1. Skolemizing blank nodes into local references 2. Dropping triples where subject.domain != domain (except blank nodes)

This ensures only authoritative data is loaded - domains can only provide data about resources they control.

activitypub.core.models.ActivityPubServer

Bases: Model

Field Types

activitypub.core.models.ReferenceField

Bases: Field

db_constraint = False class-attribute instance-attribute

Custom field that links via reference FKs instead of model PKs.

Creates a through table that links source_reference_id to target_reference_id, allowing queries without requiring the source instance to have a pk.

This field should only be used on models that have a 'reference' ForeignKey to the Reference model.

Example

class ObjectContext(AbstractContextModel): reference = models.OneToOneField(Reference, on_delete=models.CASCADE) source = ReferenceField()

Works even if obj is unsaved:

obj = ObjectContext(reference=some_ref) related_refs = obj.source.all() # Queries via obj.reference, not obj.pk

Override to prevent reverse relation setup. ReferenceField doesn't support reverse relations.

db_parameters(connection)

Override to ensure no database column is created.

db_type(connection)

ReferenceField doesn't have a database column. The relationship is stored in the through table.

deconstruct()

Return enough information to recreate the field as a 4-tuple. Used by migrations.

get_attname_column()

Override to prevent Django from creating a database column. Returns (attname, None) to indicate no column should be created.

get_internal_type()

Return the internal field type identifier.

get_lookup(lookup_name)

Override to provide custom lookups for ReferenceField filtering.

This enables QuerySet filtering like

SecV1Context.objects.filter(owner=actor_ref) SecV1Context.objects.filter(owner__in=[ref1, ref2]) SecV1Context.objects.filter(owner__isnull=True)

get_prep_value(value)

Convert Reference instances to their PKs for database queries.

m2m_field_name()

Return the name of the FK field on the through table that points to the source model.

For ReferenceField, this is 'source_reference' instead of the default.

m2m_reverse_field_name()

Return the name of the FK field on the through table that points to the target model.

For ReferenceField, this is 'target_reference' instead of the default.

m2m_reverse_target_field_name()

Return the name of the field on the source model.

m2m_target_field_name()

Return the name of the field on the target model.

to_python(value)

Convert database value to Python object. For ReferenceField, this is handled by the descriptor/manager.

activitypub.core.models.RelatedContextField

Identity and User Domain

activitypub.core.models.Identity

Bases: Model

activitypub.core.models.UserDomain

Bases: Model

ActivityStreams Context Models

These models store ActivityStreams 2.0 vocabulary data attached to references.

Core Types

activitypub.core.models.LinkContext

Bases: AbstractContextModel

activitypub.core.models.as2.AbstractAs2ObjectContext

Bases: AbstractContextModel

ActivityStreams 2.0 vocabulary context. Stores AS2-specific fields like name, type, published, actor, etc.

activitypub.core.models.ActorContext

Bases: BaseAs2ObjectContext

activitypub.core.models.ActivityContext

Bases: BaseAs2ObjectContext

validate_graph(g, owner) classmethod

Validate C2S business logic rules for activities.

Parameters:

Name Type Description Default
g Graph

The RDF graph to validate

required
owner Reference

The authenticated actor posting to their outbox

required

activitypub.core.models.QuestionContext

Bases: AbstractContextModel

Collections

activitypub.core.models.CollectionContext

Bases: BaseCollectionContext

activitypub.core.models.CollectionPageContext

Bases: BaseCollectionContext

activitypub.core.models.CollectionItem

Bases: Model

Extended Properties

activitypub.core.models.EndpointContext

Bases: AbstractContextModel

activitypub.core.models.LinkRelation

Bases: Model

activitypub.core.models.RelationshipProperties

Bases: Model

activitypub.core.models.LinkedFile

Bases: Model

Reference Relationships

activitypub.core.models.fields.ReferenceRelationship

Bases: Model

Base through model for ReferenceField relationships.

This creates a many-to-many relationship between References, linking source_reference to target_reference instead of source_model to target_model.

activitypub.core.models.fields.ContextProxy

save()

Save the context if it's been modified

Context-Aware QuerySets

activitypub.core.models.managers.ContextAwareQuerySet

Bases: QuerySet

QuerySet that transparently rewrites RelatedContextField lookups into ORM joins.

Field names declared via RelatedContextField (e.g. as2, lemmy) can be used directly in filter/exclude/order_by as if they were real FK fields::

Community.objects.filter(as2__name="My Community")
Community.objects.order_by("-lemmy__distinguished")

These are rewritten to the correct reference__<related_name>__<field> paths before hitting the database. Use with_contexts() to batch-prefetch context rows and avoid N+1 queries when iterating over a queryset.

__iter__()

Populate a dedicated prefetch cache on each instance's reference.

When with_contexts() was used, the join path data is already in _state.fields_cache from select_related. We copy it into a separate _ctx_prefetch dict on the reference object so ContextProxy can use it without risk of stale data from Django's normal field cache.

with_contexts(*field_names)

Prefetch context rows for the named RelatedContextFields via select_related.

Call this when you know you'll access context data on every result row::

Community.objects.with_contexts("as2", "lemmy").filter(...)

Each name must correspond to a RelatedContextField on the model.

activitypub.core.models.managers.ContextAwareManager

Bases: Manager

activitypub.core.models.managers.ContextAwareInheritanceQuerySet

Bases: ContextAwareQuerySet, InheritanceQuerySet

ContextAwareQuerySet with select_subclasses() support for MTI models.

activitypub.core.models.managers.ContextAwareInheritanceManager

Bases: ContextAwareManager, InheritanceManager

ContextAwareManager with select_subclasses() support for MTI models.

Social Features

activitypub.core.models.FollowRequest

Bases: StatusModel, TimeStampedModel