Nested models

Embed another TripleModel as a field value (for example a Mailbox inside Person). TripleModel exports the child’s triples and links the parent to the child subject.

Optional nested field

mbox: Mailbox | None = None exports nothing when None. Under sync_to_graph(..., mode="replace"), the parent’s link triple is removed when you clear mbox.

Blank node embedding (experimental)

class Rdf:
    embed = "bnode"

The child is exported with a fresh blank node subject; import loads a bounded description around that node. replace/patch cleanup removes stale blank-node subgraphs.

With the default blank_node_policy="fresh", every replace or patch treats existing nested blank nodes as stale and re-exports them, even when the nested Python value is unchanged. Use blank_node_policy="stable" to keep the same blank node id across syncs when content is unchanged, or prefer IRI embed for stable round-trips and linking across graphs.

Multiple parents, one graph

from triplemodel import models_to_graph

people = [alice, bob]  # each with its own Mailbox
graph = models_to_graph(people)
by_slug = {p.slug: p for p in Person.all_from_graph(graph)}

Each child mailbox keeps a distinct subject IRI when slug differs on the child model.

Full example

See Examples (examples/exit_criteria_03.py) for language tags, nested address, RDF list nick, and sync with replace.

Next: Namespaces and CURIEs →