Skip to main content

1 Django Loose FK

Version:

2.0.0

Source:

https://github.com/maykinmedia/django-loose-fk

Keywords:

ForeignKey, URL reference, decentralization, integrity

Build status Code quality checks black Coverage status

python-versions django-versions pypi-version

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)

Source distribution for django-loose-fk 2.0.0
File Size Uploaded
django_loose_fk-2.0.0.tar.gz 29.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-loose-fk 2.0.0
File Interpreter ABI Platform
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 log

Release 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

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1

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