Skip to main content

DRF extender for django models.

Project description

lint deploy pypi

Django Model Serializer package

This package provides ability for developers to create serializers directly from models.

Installation

Install using pip:

pip install django-modelserializer  

Usage

Models file:

from django.db import models

from django_modelserializer import APIMixin, APIModel, APIField
from rest_framework import serializers

class MyParentModel(APIMixin, models.Model):
    """You can inherit from APIMixin that provides serializer constructor method."""
    name = models.CharField(max_length=64)
    description = models.TextField()

	api_fields = [
		APIField('name'),
		APIField('description'),
	]

class MyModel(APIModel):
    """You can inherit from APIModel, that is inherited from APIMixin and models.Model"""
    title = models.CharField(max_length=40)
    parent = models.ForeignKey(MyParentModel, on_delete=models.CASCADE)
  
    api_fields = [
        # Plain serialize title as rest_framework does it out of the box.
        APIField('title'),
        # Serializer from foreign key field's model will be calculated automatically,
        # if ForeingKey related model is subclass of APIMixin(or APIModel).
        APIField('parent'),
        # You can specify custom serializer for field.
        APIField('parent_id', serializers.IntegerField(source='parent.id')),
    ]

Get serializer:

from .models import MyModel

MyModelSerializer = MyModel.get_serializer_class()

Here, MyModelSerializer is equal to:

from rest_framework import serializers
from .models import MyParentModel


class MyParentModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = MyParentModel
        fields = ('name', 'description')

class MyModelSerializer(serializers.ModelSerializer):
    parent = MyParentModelSerializer()

    class Meta:
        fields = ('title', 'parent', 'parent_id')

Use in views:

from rest_framework.generics import RetrieveAPIView

from .models import MyModel


class MyModelRetrieveView(RetrieveAPIView):
    serializer_class = MyModel.get_serializer_class()
    queryset = MyModel.objects.select_related('parent').all()

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-extended-0.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

drf_extended-0.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file drf-extended-0.1.tar.gz.

File metadata

  • Download URL: drf-extended-0.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for drf-extended-0.1.tar.gz
Algorithm Hash digest
SHA256 bc4e56461b378fc40e39a943f1e3026ec3b6380d9fdd84096f7d75fe2953b066
MD5 bc4ac0cb943b492dc1b9abe9ec3f7a7d
BLAKE2b-256 0310d1f0dc40d5a2d526a3245fad74a756b8ec3122cc71c6eae5c425594b8199

See more details on using hashes here.

File details

Details for the file drf_extended-0.1-py3-none-any.whl.

File metadata

  • Download URL: drf_extended-0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for drf_extended-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b82b49f9de5efd89e60d4ba1b342e5b954379103d3fb88bf163337b85d0922b
MD5 36f08bc5e35b688725825c0bbaaa4ec5
BLAKE2b-256 8055cc87104472c70cdca81d343f4b8b820fd0e731bfccee465cad06fda18d0c

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