Skip to main content

JSON API specification for Django services

Project description

django-json-api

PyPI version codecov Reveal License: MIT

django-json-api uses Django's ORM interfaces as inspiration to serialize, request and deserialize entities from databases of other microservices using (and benefiting from) the JSON:API specification.

Installation

To install via pip:

pip install django-json-api

You can also install from the source:

git clone git@github.com:reveal-co/django-json-api.git
cd django-json-api
git checkout main
pip install -e .

Getting started

Suppose you have a django.db.models.Model class inside microservice A, such as:

from django.db import models

class Company(models.Model):
    name = models.CharField(max_length=256)
    domain = models.CharField(max_length=256)
    deleted_at = models.DateTimeField(null=True, default=None)

If you wish to consume it from microservice B, first add this inside the aforementioned model's definition:

    class JSONAPIMeta:
        resource_name = 'companies'

and define an instance of a django_json_api.models.JSONAPIModel inside microservice B:

from django_json_api.models import JSONAPIModel
from django_json_api.fields import Attribute

class Company(JSONAPIModel):
    class Meta:
        api_url = MICROSERVICE_A_API_URL
        resource_type = 'companies'

    name = Attribute()
    domain = Attribute()

PS: api_url expects a url with protocol (i.e. starting with http(s)://) and ending with a trailing slash /.

Now, querying companies from microservice B is as easy as:

  Company.objects.all()
  Company.objects.filter(name="Reveal")
  Company.objects.iterator()
  ...

You can also have entities in one microservice relate to entities in another by leveraging both RelatedJSONAPIField and WithJSONAPIQuerySet. Take a look at this model definition from microservice B:

from django.db import models
from django_json_api.django import RelatedJSONAPIField


class User(models.Model):
    name = models.CharField(max_length=256)
    company = RelatedJSONAPIField(json_api_model=Company)
    deleted_at = models.DateTimeField(null=True, default=None)

Here, Company is the JSONAPIModel defined above. This makes it possible, when querying for a user, to also fetch its related company:

user = User.objects.get(pk=1)
user.company # This will be resolved through an underlying HTTP request

In case of larger querysets, you might want to prefetch the relations as you do with django's prefetch_related. For that, you imbue User's manager using WithJSONApiQuerySet, which will grant the manager a new method: prefetch_jsonapi.

If the remote microservice API supports PATCH request, you can save a record's attributes:

user = User.objects.get(pk=1)
print(user.name)  # Joe
user.name = "Jack"
user.save(update_fields=["name"]) # This will perform a PATCH HTTP request
updated_user = User.from_cache(pk=1)
print(updated_user.name)  # Jack: the updated record with its new attributes is cached

License

MIT

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-json-api-0.2.7.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

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

django_json_api-0.2.7-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file django-json-api-0.2.7.tar.gz.

File metadata

  • Download URL: django-json-api-0.2.7.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.2

File hashes

Hashes for django-json-api-0.2.7.tar.gz
Algorithm Hash digest
SHA256 4fc115bd5d6e24064db6fc65b6d0678d5fd718df38e7ea018c8489184abbab49
MD5 e8371f6cb4ae9c9c1af02861f1c5cb0d
BLAKE2b-256 f8ee5d7aa6e58c1085279943a2a2ce1ba4aa00b7123125c64fb37285f6539173

See more details on using hashes here.

File details

Details for the file django_json_api-0.2.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_json_api-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 634401dcee0c15153a04f15c7c6145a03c7c0f43b0ccd978aef15195969c072c
MD5 12642522dce1276b393f8a42deee1dba
BLAKE2b-256 384fe975a4ec4377278eceb463f8a334234fdd44d5a3f04b9c25bb09ec3de126

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