A RDF library for Django models
Project description
Django RDF
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 withclass_iri,namespace, andgraph_iriconfigured through aMetainner class. Defaults toskos:Conceptand uses the namespace and graph IRIs from Django settings.- Property types —
DataPropertyfor typed literals,LangStringPropertyforrdf:langString(using theLangStringdataclass),ObjectPropertyfor instance-to-instance links (with lazy"self"and string-name target resolution), andURIPropertyfor raw IRIs. Each supportsmany=True/Falsecardinality. - SKOS defaults — properties named
pref_label,alt_label,broader,narrower, etc. resolve to their SKOS predicates automatically; no explicitpredicate=needed for the conventions. RDFManager+ lazyRDFQuerySet— Django-styleobjects.create,objects.get,objects.all,objects.filter.saveis idempotent and overwrites stale triples in a single SPARQL update;deletestrips every triple for the IRI.- Backends — ships an
InMemoryBackend(rdflib, good for tests and local development) and aFusekiBackend(SPARQL 1.1 HTTP, works against Apache Jena Fuseki, GraphDB, Blazegraph, Stardog). NamespaceRegistry— register prefixes throughDJANGORDF_NAMESPACESand resolve CURIEs like"skos:Concept"into fullURIRefobjects.- 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file djangordf-0.9.0.tar.gz.
File metadata
- Download URL: djangordf-0.9.0.tar.gz
- Upload date:
- Size: 71.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbcc90de1300fc344ef8caebfcbbeb88538b0b962c7e4d45d3c47c32ab6fbd9a
|
|
| MD5 |
f7cf271e8d8287a1a474e1504c94d498
|
|
| BLAKE2b-256 |
b66274e4eb13ace01b00c1bc9435b72bd38aed233625cf320803101028ab60f2
|
Provenance
The following attestation bundles were made for djangordf-0.9.0.tar.gz:
Publisher:
python-publish.yml on judaicalink/djangordf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
djangordf-0.9.0.tar.gz -
Subject digest:
dbcc90de1300fc344ef8caebfcbbeb88538b0b962c7e4d45d3c47c32ab6fbd9a - Sigstore transparency entry: 1839159772
- Sigstore integration time:
-
Permalink:
judaicalink/djangordf@3370429303e5f408627319008c0eaaed4df060ff -
Branch / Tag:
refs/tags/v0.9.0 - Owner: https://github.com/judaicalink
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3370429303e5f408627319008c0eaaed4df060ff -
Trigger Event:
release
-
Statement type:
File details
Details for the file djangordf-0.9.0-py3-none-any.whl.
File metadata
- Download URL: djangordf-0.9.0-py3-none-any.whl
- Upload date:
- Size: 41.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e9c48d05b30539e44157bcca01f2f4d28954f5f5b07e591b1de97db02b26f99
|
|
| MD5 |
5e64bc52ae0bfaf0b586779688e946e4
|
|
| BLAKE2b-256 |
028d7326e8f377be6c932cc3720159078e69bdce9d9f50f3f33a537fb0fcb70c
|
Provenance
The following attestation bundles were made for djangordf-0.9.0-py3-none-any.whl:
Publisher:
python-publish.yml on judaicalink/djangordf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
djangordf-0.9.0-py3-none-any.whl -
Subject digest:
5e9c48d05b30539e44157bcca01f2f4d28954f5f5b07e591b1de97db02b26f99 - Sigstore transparency entry: 1839159813
- Sigstore integration time:
-
Permalink:
judaicalink/djangordf@3370429303e5f408627319008c0eaaed4df060ff -
Branch / Tag:
refs/tags/v0.9.0 - Owner: https://github.com/judaicalink
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3370429303e5f408627319008c0eaaed4df060ff -
Trigger Event:
release
-
Statement type: