Skip to main content

Django Loose FK handles local or remote "ForeignKey" references.

Project description

Version: 0.7.4
Keywords:ForeignKey, URL reference, decentralization, integrity

build-status Requirements status 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   Features

  • Always work with Django model instances
  • Pluggable interface to fetch remote objects
  • Automatically supports DRF Hyperlinked serializers and serializer fields

2   Installation

2.1   Requirements

  • Python 3.7 or above
  • setuptools 30.3.0 or above
  • Django 2.0 or newer

2.2   Install

pip install django-loose-fk


You must also make sure ALLOWED_HOSTS is a list of actual domains, and not a wildcard. When loose-fk gets a URL to load, it first looks up if the domain is a local domain and if so, will load the actual local database record.

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()


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.

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(

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-loose-fk, version 0.7.4
Filename, size File type Python version Upload date Hashes
Filename, size django_loose_fk-0.7.4-py2.py3-none-any.whl (33.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size django-loose-fk-0.7.4.tar.gz (24.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page