This release is a pre-release and may not be stable for production use.
drf-nest
Writable nested serialisers for Django Rest Framework
The sample
A sample project is included which implements a retail type use case.
- Sales have a foreign key to a store
- Sale items have foreign keys to the sale.
To run sample:
pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser super super@super.com super
python manage.py runserver
To use
python setup.py install
In your serialisers.py file import extensions:
from drf_nest.serializers import ExtendedHyperlinkedSerialiser
from drf_nest.serializer_fields import ExtendedModelSerialiserField
For each model serialiser using nested field use the ExtendedHyperlinkedSerialiser
class SaleSerialiser(ExtendedHyperlinkedSerialiser):
For each nested representation in the parent object use the ExtendedModelSerialiserField
class SaleSerialiser(ExtendedHyperlinkedSerialiser):
sale_items = ExtendedModelSerialiserField(
SaleItemSerialiser(),
many=True,
required=False,
allow_null=True)
We won't know the foreign key to fulfill the relationship if we are creating the parent so in each sub objects serialiser, the parent object must be made optional.
class SaleItemSerialiser(ExtendedHyperlinkedSerialiser):
sale = serializers.HyperlinkedRelatedField(
required=False,
view_name='sale-detail',
queryset=Sale.objects.all()
)
How does it work
The serialiser field overrides the to internal function to return a dictionary (or list of dictionaries) rather than the django model instance. This is done because the fields do not know if the parent exists already but may have a required foreign key constraint.
Features
See the sample project tests for example POST requests.
- During POST, PUT and PATCH user can specify nested object either by URL or full serialised representation
- Adds type field to allow for generic foreign keys (in development)
- Serialisation of model with foreign key
- Serialisation of model with reverse relationship
- Serialisation of model with many to many relationship
Metadata
Release files for drf-nest 0.1a5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_nest-0.1a5.tar.gz | 15.6 kB | Without provenance | Details for drf_nest-0.1a5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_nest-0.1a5-py3-none-any.whl | Python 3 | none | any | Details for drf_nest-0.1a5-py3-none-any.whl |
Total release size: 45.0 kB
Release files / drf_nest-0.1a5.tar.gz
| Download URL | drf_nest-0.1a5.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
960969e98438edd61643cb6fd446934ce7c64bef7fd79ac6fb3e228c65e10cf0
|
|
BLAKE2b-256 checksum How to use checksums |
d4776a2c03d39715690807ef4997002b890ed384abeac088fb610b2b68041202
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5
|
Release files / drf_nest-0.1a5-py3-none-any.whl
| Download URL | drf_nest-0.1a5-py3-none-any.whl |
|---|---|
| Size | 29.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b30e4bce10c383a26fda522c70cacdb1633ee6af3189b017585c76a359d2a409
|
|
BLAKE2b-256 checksum How to use checksums |
aef8979443e0a08ded8d4298920fb496990bd3876fc037fe0e8a07d1357da42a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5
|
Security for drf-nest 0.1a5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.