1 Django Loose FK
- Version:
2.0.0
- Source:
- Keywords:
ForeignKey, URL reference, decentralization, integrity
Django Loose FK handles local or remote “ForeignKey” references.
In a decentralized API landscape various providers can offer the same type of data, while your own API also provides this. The django model field allows you to handle this transparently and present a unified, clean Python API.
1.1 Features
Always work with Django model instances
Automatically added check constraints
Pluggable interface to fetch remote objects
Automatically supports DRF Hyperlinked serializers and serializer fields
1.2 Installation
1.2.1 Requirements
Python 3.12 or higher
Setuptools 77.0.0 or higher
Django 5.2 or newer
Only the PostgreSQL database is supported
1.2.2 Install
pip install django-loose-fk
1.3 Usage
At the core sits a (virtual) django model field.
from django_loose_fk.fields import FkOrURLField
class SomeModel(models.Model):
name = models.CharField(max_length=100)
class OtherModel(models.Model):
local = models.ForeignKey(SomeModel, on_delete=models.CASCADE, blank=True, null=True)
remote = models.URLField(blank=True)
relation = FkOrURLField(fk_field="local", url_field="remote")
You can now create objects with either local instances or URLs:
some_local = SomeModel.objects.get()
OtherModel.objects.create(relation=some_local)
OtherModel.objects.create(relation="https://example.com/remote.json")
Accessing the attribute will always yield an instance:
>>> other = OtherModel.objects.get(id=1) # local FK
>>> other.relation
<SomeModel (pk: 1)>
>>> other = OtherModel.objects.get(id=2) # remote URL
>>> other.relation
<SomeModel (pk: None)>
In the case of a remote URL, the URL will be fetched and the JSON response used as init kwargs for a model instance. The .save() method is blocked for remote instances to prevent mistakes.
1.3.1 Loaders
Loaders are pluggable interfaces to load data. The default loader is django_loose_fk.loaders.RequestsLoader, which depends on the requests library to fetch the data.
You can specify a global default loader with the setting DEFAULT_LOOSE_FK_LOADER
DEFAULT_LOOSE_FK_LOADER = "django_loose_fk.loaders.RequestsLoader"
or override the loader on a per-field basis:
from django_loose_fk.loaders import RequestsLoader
class MyModel(models.Model):
...
relation = FkOrURLField(
fk_field="local",
url_field="remote",
loader=RequestsLoader()
)
1.3.2 Local and remote urls
If several services are hosted within the same domain, it could be tricky to separate local and remote urls. In this case an additional setting LOOSE_FK_LOCAL_BASE_URLS can be used to define an explicit list of allowed prefixes for local urls.
LOOSE_FK_LOCAL_BASE_URLS = [
"https://api.example.nl/ozgv-t/zaken/",
"https://api.example.nl/ozgv-t/catalogi/",
]
Metadata
Release files for django-loose-fk 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_loose_fk-2.0.0.tar.gz | 29.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_loose_fk-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 53.3 kB
Release files / django_loose_fk-2.0.0.tar.gz
| Download URL | django_loose_fk-2.0.0.tar.gz |
|---|---|
| Size | 29.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ede62a198cb6321e00527851a0c5f1d447da533823a33dca4b2483db5b9013d
|
|
BLAKE2b-256 checksum How to use checksums |
424b780303ea6e109b666ab661170bbc29d357cce89c4f3b18a227a17c1ceec9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 29, 2026.
Transparency logRelease files / django_loose_fk-2.0.0-py3-none-any.whl
| Download URL | django_loose_fk-2.0.0-py3-none-any.whl |
|---|---|
| Size | 23.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7cb71dca158c8870cd2fb0c5a466ffb92ab58ebd21a7fcafcc4fc3bc6acc2090
|
|
BLAKE2b-256 checksum How to use checksums |
27ade46fc3be582f6bb9e8f993d30322ea6c11de289d91e4f716b7dd6703d236
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 29, 2026.
Transparency log