Skip to main content

Django queryset and serialization together

Project description

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:

class BusSerializer(BaseSerializer):
    select_related = ['company']

    def serialize_object(self, obj):
        return {
            'id': self.id,
            'company': {
                'name': self.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': self.id,
            'company': {
                'name': self.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.

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_qserializer-0.2.8.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

django_qserializer-0.2.8-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file django_qserializer-0.2.8.tar.gz.

File metadata

  • Download URL: django_qserializer-0.2.8.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for django_qserializer-0.2.8.tar.gz
Algorithm Hash digest
SHA256 6a24c3e60e852c936bf63cada4305c5804958ec32b31f19f2c177cf0f9cd9cbb
MD5 d420758b51b742e7b71124dbf5150964
BLAKE2b-256 14559340e26adc88c469969b9a406ebb6bc2b181bb00c2c4bc29bd91e5d692ba

See more details on using hashes here.

File details

Details for the file django_qserializer-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: django_qserializer-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for django_qserializer-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d4bf998a32d832bae7e9aab91098ccf12c6c244731b5cb3ac6065ba85ee69218
MD5 73a116e403f796e977a6e3a1b6ed37e4
BLAKE2b-256 7f86365605ef60dcf640e11b0d9a0b5b6e4e7f17b19a49427add920c4ad2de3e

See more details on using hashes here.

Supported by

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