Skip to main content

1 Django Loose FK

Version:

2.0.1

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.1

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.1
File Size Uploaded
django_loose_fk-2.0.1.tar.gz 29.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-loose-fk 2.0.1
File Interpreter ABI Platform
django_loose_fk-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 53.7 kB

Release files / django_loose_fk-2.0.1.tar.gz

Download URL django_loose_fk-2.0.1.tar.gz
Size 29.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6c075007570a72e4ce3257a48a531366ec5e81841526e947343e2f7705592dfc
BLAKE2b-256 checksum
How to use checksums
bc411c0d5b4d5706b4b552aeb29d96aeb5ea6428800221e3911b844247781b4e
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 Oct 15, 2026.

Transparency log

Release files / django_loose_fk-2.0.1-py3-none-any.whl

Download URL django_loose_fk-2.0.1-py3-none-any.whl
Size 23.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f34a16d022787741726504bacc7aac61a0a6e4a9bb15d9f6baae08c2265f129
BLAKE2b-256 checksum
How to use checksums
022a7450680dd8a7c0f7599da34f6d367535755ee187d4d728f6f5b2b1999ea5
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 Oct 15, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

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