django-qserializer
Django QSerializer started as an internal Buser project to improve our serialization process.
Database queries and serialization are two separated steps, but really ORM coupled. Relationships must be fetched from database before serialization, but Django don't have an easy way to setup that.
Start with a custom manager SerializableManager.
from django.db import models
from django_qserializer import SerializableManager
class Company(models.Model):
name = models.CharField(max_length=64)
class Bus(models.Model):
objects = SerializableManager()
company = models.ForeignKey(Company, on_delete=models.SET_NULL)
A basic serializer implementation would be:
from django_qserializer import BaseSerializer
class BusSerializer(BaseSerializer):
select_related = ['company']
def serialize_object(self, obj):
return {
'id': obj.id,
'company': {
'name': obj.company.name,
}
}
Add the serializer to your queryset as:
buses = Bus.objects.to_serialize(BusSerializer).all()
for bus in buses:
# The serialize method is bound to BusSerializer.serialize_object.
print(bus.serialize())
API
BaseSerializer.select_related
List of model fields to add to queryset with a select_related call.
BaseSerializer.prefetch_related
List of model fields to add to queryset with a prefetch_related call.
class BusSerializer(BaseSerializer):
prefetch_related = ['company']
def serialize_object(self, obj):
return {
'id': obj.id,
'company': {
'name': obj.company.name,
}
}
BaseSerializer.prepare_queryset
Callable to change the queryset. It is possible to implement select_related
and prefetch_related attributes with it, but they work together with
prepare_queryset.
class BusSerializer(BaseSerializer):
select_related = ['company']
def prepare_queryset(self, qs):
return qs.annotate(state=Value('broken'))
BaseSerializer.prepare_objects
Prepare objects after they are loaded to memory. Add data in bulk to them, like fetching information from cache and attaching to loaded objects.
BaseSerializer.serialize_object
Required implementation. It converts the Django model to a serializable dict. Avoid slow calls here because it will cause N+1 issues.
BaseSerializer.serialize
Execute serialize_object for each model object.
Development
To run the project, it is necessary the following tools:
- Python 3.9 or higher
To create a virtual environment, run
python3 -m venv .venv
To activate the virtual environment, run
source .venv/bin/activate
To install the project's requirements in the virtual environment, run
pip install -e .\[test\]
To deactivate the virtual environment, run deactivate.
Run the commands of the following sections with the virtual environment active.
Quality
The quality metrics of the project are reproduced by the continuos integration (CI) pipeline of the project. CI configuration in .github/workflows/ci.yml file.
Tests and linter
To run tests, coverage report and linter, run
pytest
To see the html report, check htmlcov/index.html.
Tests and coverage configuration in setup.cfg file, at [tool:pytest] section.
Linter configuration in setup.cfg file, at [flake8] section.
License
This repository is licensed under the terms of MIT License.
Metadata
Release files for django-qserializer 0.2.9
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 |
|---|---|---|---|---|
| django_qserializer-0.2.9.tar.gz | 8.4 kB | Without provenance | Details for django_qserializer-0.2.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_qserializer-0.2.9-py3-none-any.whl | Python 3 | none | any | Details for django_qserializer-0.2.9-py3-none-any.whl |
Total release size: 17.3 kB
Release files / django_qserializer-0.2.9.tar.gz
| Download URL | django_qserializer-0.2.9.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e576a3e1b704aa782f6ed2116e7745eb2d210d351412f17db66e32f2cdf1357
|
|
BLAKE2b-256 checksum How to use checksums |
955d062e0517aba387a90c3f50909771fe2dd081ae39b38fb6c66b2e51b2ad55
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Release files / django_qserializer-0.2.9-py3-none-any.whl
| Download URL | django_qserializer-0.2.9-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e7bc9d7561eade8186a7cbbf95fa616f64417eb4da8d1ea6998f93d441d1ce10
|
|
BLAKE2b-256 checksum How to use checksums |
15939603d0456a19047d4d71a941870823057b968be0dcbab32a5d86b88b3f1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Security for django-qserializer 0.2.9
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.