Skip to main content

TODO

Project description

Version:
0.4.0
Source:

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

Keywords:

ForeignKey, URL reference, decentralization, integrity

PythonVersion:

3.7

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

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

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-0.4.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_loose_fk-0.4.0-py2.py3-none-any.whl (21.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-loose-fk-0.4.0.tar.gz.

File metadata

  • Download URL: django-loose-fk-0.4.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for django-loose-fk-0.4.0.tar.gz
Algorithm Hash digest
SHA256 f2daa4ffc99ae14191f7f059ee6d2149774e4098b9cc2e3a713eaaafa7cdfa04
MD5 411ad7d761d8602ad5b72a4d6c4e5111
BLAKE2b-256 9bb5db1a4fdaaa31b83fd27e8f7b8ec11690c5e0a1489d4d2f63f108e6223f5e

See more details on using hashes here.

File details

Details for the file django_loose_fk-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_loose_fk-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for django_loose_fk-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 31ed3ee75cda63a4c243aa65fb3686b7cb4ee30a8059e9f5f3747c36efa9c179
MD5 f0111dfc7da0d86ce079768e60e9976d
BLAKE2b-256 2e049785424eb2373b26fba92e3860311af988d52837505a1963206474269057

See more details on using hashes here.

Supported by

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