Skip to main content

Django RDF

Maintenance

GitHub license

forthebadge made-with-python

PyPI - Downloads

PyPI - Version

djangordf is a Django library for managing RDF data with a declarative model layer. Domain classes are written like Django models but persist as triples in a SPARQL 1.1 triple store, with built-in SKOS conventions, language-tagged literals, and a lazy queryset that mirrors Django's manager API.

Full reference documentation: https://djangordf.readthedocs.io/.

Features

  • RDFModel — declarative base class with class_iri, namespace, and graph_iri configured through a Meta inner class. Defaults to skos:Concept and uses the namespace and graph IRIs from Django settings.
  • Property types — DataProperty for typed literals, LangStringProperty for rdf:langString (using the LangString dataclass), ObjectProperty for instance-to-instance links (with lazy "self" and string-name target resolution), and URIProperty for raw IRIs. Each supports many=True/False cardinality.
  • SKOS defaults — properties named pref_label, alt_label, broader, narrower, etc. resolve to their SKOS predicates automatically; no explicit predicate= needed for the conventions.
  • RDFManager + lazy RDFQuerySet — Django-style objects.create, objects.get, objects.all, objects.filter. save is idempotent and overwrites stale triples in a single SPARQL update; delete strips every triple for the IRI.
  • Backends — ships an InMemoryBackend (rdflib, good for tests and local development) and a FusekiBackend (SPARQL 1.1 HTTP, works against Apache Jena Fuseki, GraphDB, Blazegraph, Stardog).
  • NamespaceRegistry — register prefixes through DJANGORDF_NAMESPACES and resolve CURIEs like "skos:Concept" into full URIRef objects.
  • Legacy export_model_to_rdf — the original Django-model-to-RDF exporter from earlier releases still works unchanged.

Installation

pip install djangordf

Add the app to INSTALLED_APPS and configure a backend:

# settings.py
INSTALLED_APPS = [
    # ...
    "djangordf",
]

DJANGORDF_BACKEND = {
    "class": "djangordf.backends.memory.InMemoryBackend",
}

DJANGORDF_DEFAULT_NAMESPACE = "http://example.org/data/"
DJANGORDF_DEFAULT_GRAPH = "http://example.org/graph/default"

DJANGORDF_NAMESPACES = {
    "ex": "http://example.org/vocab/",
}

Talking to a real triple store

Swap in the Fuseki backend (or any SPARQL 1.1 endpoint):

DJANGORDF_BACKEND = {
    "class": "djangordf.backends.fuseki.FusekiBackend",
    "endpoint": "http://localhost:3030/judaicalink",
    # optional:
    "user": "admin",
    "password": "secret",
}

The repository ships a docker-compose.yml that boots a local Fuseki for development; opt-in integration tests behind the @pytest.mark.fuseki marker exercise the HTTP path end-to-end.

Usage

Define a model:

from djangordf import (
    RDFModel,
    LangStringProperty,
    ObjectProperty,
)
from djangordf.namespaces import LangString


class Term(RDFModel):
    pref_label = LangStringProperty(many=True)
    alt_label = LangStringProperty(many=True)
    broader = ObjectProperty("self", many=True)

No class Meta, no predicate= arguments: Term defaults to skos:Concept, and pref_label / alt_label / broader resolve to their SKOS predicates through the convention map. Mint, query, link:

buch = Term.objects.create(
    pref_label=[LangString("Buch", "de"), LangString("Book", "en")],
)
roman = Term.objects.create(
    pref_label=[LangString("Roman", "de")],
    broader=[buch],
)

reloaded = Term.objects.get(roman.iri)
assert reloaded.broader[0].iri == buch.iri
assert any(
    ls.lang == "en" and ls.value == "Book"
    for ls in buch.pref_label
)

save is idempotent — calling it twice does not duplicate triples. Updates overwrite previous values in one SPARQL transaction:

buch.pref_label = [LangString("Buch (überarbeitet)", "de")]
buch.save()

Queries return a lazy RDFQuerySet; the store is only hit on iteration, len, count, or first:

for term in Term.objects.all():
    print(term.iri, term.pref_label)

assert Term.objects.filter(broader=buch).count() == 1

Explicit predicates and CURIE-based class IRIs are also supported:

from rdflib import URIRef
from rdflib.namespace import XSD

from djangordf import DataProperty, URIProperty


class Person(RDFModel):
    name = DataProperty(
        predicate=URIRef("http://xmlns.com/foaf/0.1/name"),
        datatype=XSD.string,
    )
    homepage = URIProperty(
        predicate=URIRef("http://xmlns.com/foaf/0.1/homepage"),
    )

    class Meta:
        class_iri = "foaf:Person"   # resolved via NamespaceRegistry
        namespace = "http://example.org/people/"
        graph_iri = "http://example.org/graph/people"

Walking-skeleton example

A self-contained, runnable end-to-end example lives at examples/walking_skeleton.py. It mirrors the design spec's acceptance script and exits 0 against the default in-memory backend:

python examples/walking_skeleton.py

Legacy Django-to-RDF export

The original export_model_to_rdf(QuerySet, ...) function is still available for one-shot dumps of relational Django data into a Turtle file; see djangordf/functions.py and the existing tests in tests/test_export.py.

Settings reference

Setting Purpose Default
DJANGORDF_BACKEND Triple-store backend config (dict with class plus kwargs) In-memory backend
DJANGORDF_DEFAULT_NAMESPACE Used to mint IRIs when Meta.namespace is silent urn:djangordf:<model>:
DJANGORDF_DEFAULT_GRAPH Used when Meta.graph_iri is silent urn:djangordf:default
DJANGORDF_NAMESPACES prefix -> uri map read into the NamespaceRegistry at app ready {}

License

This project is licensed under the MIT License.

Metadata

Release files for djangordf 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djangordf 1.0.0
File Size Uploaded
djangordf-1.0.0.tar.gz 86.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djangordf 1.0.0
File Interpreter ABI Platform
djangordf-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 142.5 kB

Release files / djangordf-1.0.0.tar.gz

Download URL djangordf-1.0.0.tar.gz
Size 86.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d0e469fd16ae932c24bb5d030060492a444c6885402eb5e4d47e3fa8aa8ac6a3
BLAKE2b-256 checksum
How to use checksums
aacb582f0841a9c94f777fba7daa5f458ca2a9a662fc701844ec784e731b5b99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 17, 2026.

Transparency log

Release files / djangordf-1.0.0-py3-none-any.whl

Download URL djangordf-1.0.0-py3-none-any.whl
Size 56.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ceb06f898e5240aa81d38229d7fda95a7da13b30905d283d16d1ee8bc0de2042
BLAKE2b-256 checksum
How to use checksums
4219a69f2b727f89cb57514ce60352edc56b45823b66129c2a98c604e7dc330e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page