Skip to main content

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()

Metadata

Release files for drf-extended 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-extended 0.1
File Size Uploaded
drf-extended-0.1.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-extended 0.1
File Interpreter ABI Platform
drf_extended-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.6 kB

Release files / drf-extended-0.1.tar.gz

Download URL drf-extended-0.1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bc4e56461b378fc40e39a943f1e3026ec3b6380d9fdd84096f7d75fe2953b066
BLAKE2b-256 checksum
How to use checksums
0310d1f0dc40d5a2d526a3245fad74a756b8ec3122cc71c6eae5c425594b8199
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / drf_extended-0.1-py3-none-any.whl

Download URL drf_extended-0.1-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b82b49f9de5efd89e60d4ba1b342e5b954379103d3fb88bf163337b85d0922b
BLAKE2b-256 checksum
How to use checksums
8055cc87104472c70cdca81d343f4b8b820fd0e731bfccee465cad06fda18d0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page