Django Retriever
The Simple Way to Interface JSON and Django
Overview
The retriever is an interface used to define how to map a JSON object to a Django model. The retriever takes care of mutating the JSON structure, connecting foreign objects, and creating or updating the resulting Django model.
There are several components that must be defined in the retriever to complete the job,
modelthe Django model with which the retriever is intended to interfaceidthe list of field names in the Django model that should be used to determine whether an object is already in the database. These can be thought of unique fields. If they are not actually unique, the retriever will react by not saving the JSON object.structuresthe critical component of the retriever. Thestructuresattribute is the interface between the JSON document and the Django model. The interface is a nested structure of lists and dictionaries, and must nest to a series of tuple/list objects in the following format,namethe name of the key in the JSON documentforeign_structures(defined below) the list of foreign structures to define how a key in the JSON document must be used to relate Django modelsstructures(defined below) the list of mappings to determine how the JSON document value must be mutated and saved to the Django model
Definitions
ForeignStructuremodelthe foreign modelidthe primary key field name in the foreign modelid2the field name in the current model that is mapped to the primary key in the foreign model (id)Structurethe normal structure definition for the JSON document, because the value might very well map to another field on the foreign model
Structurenamethe new name of the JSON document, in the case the JSON and the Django model name the field differentlyfuncthe mutation to perform on the JSON document. Can be one ofint,str,bool, or a custom function
Installation
pip install django-retriever
Contributing
Before contributing, please install the test and dev versions of
the library,
pip install django-retriever[dev,test]
Please run tests using (in the root project directory)
python -m pytest tests
Once the package is ready to be uploaded, please complete the following in order,
- Increment the
versiontemplate inpyproject.toml - Set the
PYPI_TOKENenvironment variable - Run
./bin/twinefrom the root project directory
Usage
The best explanation is going to be through an implementation, so please consider the following case. The JSON object includes information about a product image, and is taken from a public API.
Consider the JSON document,
{
"results": [
{
"raw": {
"ec_sku": "333333",
"image": "https://image3.jpeg"
}
}
{
"raw": {
"ec_sku": "333333",
"image": "https://image3.jpeg"
}
}
{
"raw": {
"ec_sku": "333333",
"image": "https://image3.jpeg"
}
}
]
}
The JSON document should be loaded into a python object,
using a parser of your choice. The following code in
models.py and retriever.py is a sample that could be
used to parse the JSON document into some database objects
through the Django ORM.
# models.py
from django.db import models
class Product(models.Model):
id = models.AutoField(
primary_key=True)
sku = models.CharField(
max_length=64)
class Image(models.Model):
product = models.ForeignKey(
Product,
on_delete=models.CASCADE,
related_name="images")
image = models.ImageField(
# storage, etc.
...)
source_url = models.CharField(
max_length=512)
# retrievers.py
from retriever import Retriever
from .models import (
Image,
Product
)
class ProductRetriever(Retriever):
model = Product
id = ["sku"]
structures = [
{
"results": [
{
"raw": [
[
"ec_sku",
[],
[
"sku",
None
]
]
]
}
]
}
]
class ImageRetriever(Retriever):
model = Image
id = ["product_id", "source_url"]
structures = [
{
"results": [
{
"raw": [
[
# JSON name
"ec_sku",
# foreign structures
[
# foreign model
Product,
# id and foreign id field name
["id"],
["product_id"],
# normal structure, the `ec_sku` field is
# clearly a map to the unique field `sku`, not `id` # itself. Note that `sku` should be unique or the
# structure will not be created and an error should
# be propagated
[
"sku",
None
],
],
# structures. Note `sku` is not in the `Image` model,
# we are not interested in mapping it
[],
],
[
# JSON name
"image",
# no foreign structures
[],
# map it to `source_url` field name, no
# mutation required
[
"source_url",
None,
],
],
]
},
]
}
]
If you've loaded in the JSON document and defined the retrievers correctly, you can now simply save the JSON document to the database,
from .retrievers import *
json_object = {
"results": [
...
]
}
ProductRetriever(
batch_size=5,
default=[],
strict=True
).save(json_object)
ImageRetriever(
batch_size=5,
default=[],
strict=True
).save(json_object)
The retrievers have used the ec_sku field in the JSON
document to find the Product object that corresponds to
the correct Image object. In this way, a JSON document can
be decomposed into several retrievers, and the definitions
can be isolated. Note, the ProductRetriever should be called
before the ImageRetriever, or there will be no Product objects
in the database to find.
Metadata
Release files for django-retriever 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-retriever-0.0.1.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_retriever-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.2 kB
Release files / django-retriever-0.0.1.tar.gz
| Download URL | django-retriever-0.0.1.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4cf0d6bb72d7faf031f8174a1a185d947d186cadd225028037613565f2f3de5a
|
|
BLAKE2b-256 checksum How to use checksums |
bd04db38f8f76dd09b6a4bad030e3a5114ba63fb067c0280529577c8cd8cd119
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / django_retriever-0.0.1-py3-none-any.whl
| Download URL | django_retriever-0.0.1-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7591d9d6e9072aacc917de8b5ce5c3dc828d50360876ffcc6c5268e8badddd22
|
|
BLAKE2b-256 checksum How to use checksums |
69ef46cd6c44d398cbd3807770c47493a16c8f7e570cb448d78e4d342df1ec2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|