djangordf.properties

Declarative property descriptors. Each property owns its RDF serialisation (to_rdf) and deserialisation (from_rdf) and supports many=True/False cardinality.

Declarative property descriptors for RDFModel.

Property type system (issue #7). The metaclass collects Property instances at class creation, hands each one the owner class via contribute_to_class, and later delegates RDF serialisation / deserialisation to to_rdf / from_rdf per property.

class djangordf.properties.Property(predicate=None, *, many=False, required=False, default=None)[source]

Bases: object

Base class for declarative property descriptors.

Subclasses override to_rdf and from_rdf to map Python values to RDF terms and back. The base implementation is a no-op so simple direct uses of Property keep working — useful for tests that only need a predicate stub.

Parameters:
contribute_to_class(attr_name, owner_class=None)[source]
Return type:

None

Parameters:

attr_name (str)

default()[source]
to_rdf(subject, value)[source]

Return the triples this property contributes for value.

Default implementation: emit no triples. Concrete subclasses override.

from_rdf(graph, subject)[source]

Read this property’s value back out of a graph.

Default implementation returns None (scalar) or [] (many).

class djangordf.properties.DataProperty(predicate=None, *, datatype=None, many=False, required=False, default=None)[source]

Bases: Property

Typed-literal data property (xsd:string, xsd:integer, …).

Parameters:
to_rdf(subject, value)[source]

Return the triples this property contributes for value.

Default implementation: emit no triples. Concrete subclasses override.

from_rdf(graph, subject)[source]

Read this property’s value back out of a graph.

Default implementation returns None (scalar) or [] (many).

class djangordf.properties.LangStringProperty(predicate=None, *, many=False, required=False, default=None)[source]

Bases: Property

Language-tagged string property mapping to rdf:langString.

Parameters:
to_rdf(subject, value)[source]

Return the triples this property contributes for value.

Default implementation: emit no triples. Concrete subclasses override.

from_rdf(graph, subject)[source]

Read this property’s value back out of a graph.

Default implementation returns None (scalar) or [] (many).

class djangordf.properties.URIProperty(predicate=None, *, many=False, required=False, default=None)[source]

Bases: Property

Raw-IRI property (no Python wrapper, just URIRef).

Parameters:
to_rdf(subject, value)[source]

Return the triples this property contributes for value.

Default implementation: emit no triples. Concrete subclasses override.

from_rdf(graph, subject)[source]

Read this property’s value back out of a graph.

Default implementation returns None (scalar) or [] (many).

class djangordf.properties.ObjectProperty(target, predicate=None, *, many=False, required=False, default=None, inverse=None, reverse=False)[source]

Bases: Property

Link between two RDFModel instances.

target may be the target class, the string "self", or the name of a registered model class — the last two resolve lazily through djangordf.models.get_registered_model the first time target_class is accessed.

inverse names a property declared on the target class. When present, saving an instance also writes the mirror triple (target.iri, inverse_predicate, self.iri) and deletes any stale mirror triples pointing at this instance via the inverse predicate. The resolution is lazy — accessing inverse_property or inverse_predicate for the first time looks up the referenced attribute on the target class.

reverse=True declares a read-only virtual property whose triples live on the target class’s forward predicate. Saving an instance emits no triples for this property; reading hydrates target-class ghost instances by looking up subjects that point at this instance via predicate. Filter paths through a reverse segment swap subject and object so the generated SPARQL traverses the inverse direction. reverse=True is mutually exclusive with inverse=<name> (the latter implies mirror writes, which contradicts read-only semantics).

Parameters:
property target_class
property inverse_property

Resolve inverse to the matching Property on the target class. Returns None if no inverse was declared; raises ValueError if the name does not resolve.

property inverse_predicate

URIRef predicate of the resolved inverse property, or None if no inverse was declared.

to_rdf(subject, value)[source]

Return the triples this property contributes for value.

Default implementation: emit no triples. Concrete subclasses override.

from_rdf(graph, subject)[source]

Read this property’s value back out of a graph.

Default implementation returns None (scalar) or [] (many).