Skip to main content

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

Project description

Version:
1.1.2
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 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

2 Installation

2.1 Requirements

  • Python 3.10 or above

  • setuptools 30.3.0 or above

  • Django 3.2 or newer

2.2 Install

pip install django-loose-fk

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.

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

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/",
]

Project details


Download files

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

Source Distribution

django_loose_fk-1.1.2.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

django_loose_fk-1.1.2-py3-none-any.whl (37.6 kB view details)

Uploaded Python 3

File details

Details for the file django_loose_fk-1.1.2.tar.gz.

File metadata

  • Download URL: django_loose_fk-1.1.2.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for django_loose_fk-1.1.2.tar.gz
Algorithm Hash digest
SHA256 f905961eed898234b79825f9d50f492a9accb74a81de7ff4ef380a16d3708f62
MD5 c0e69f65216faa58975ede79485f45b0
BLAKE2b-256 cd83b03f2539f31a8bdd215f52bc15ab5b29c1821df14854038a6c38dc28caa1

See more details on using hashes here.

File details

Details for the file django_loose_fk-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_loose_fk-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 50ba64a3dae9b3a4c828eb8ec9d60cd06d427b999b98184450c53198449fdb41
MD5 fd20f15e9eff86dfbcf6375d345cbf5e
BLAKE2b-256 b69223df4d6ebd1977e9e67ce37a22514b7b688e62948c05b9c6a290ef2b78ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page