Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Simple HTTP PUT request handler for Django REST Framework(DRF)

Project description

drf-pretty-update

This is a collection of simple and flexible model serializer and fields for Django REST Framework which allows you to create/update your models with related nested data.

Installing

pip install drf-pretty-update

Getting Started

drf-pretty-update has two components, NestedModelSerializer and NestedField. A serializer NestedModelSerializer has update and create logics for nested fields on the other hand NestedField is used to validate data before dispatching update or create.

Using NestedField

from app.models import Location, Amenity, Property
from drf_pretty_update.serializers import NestedModelSerializer
from drf_pretty_update.fields import NestedField


class LocationSerializer(NestedModelSerializer):
    class Meta:
        model = Location
        fields = ("id", "city", "country")


class AmenitySerializer(NestedModelSerializer):
    class Meta:
        model = Amenity
        fields = ("id", "name")


class PropertySerializer(NestedModelSerializer):
    location = NestedField(LocationSerializer)
    amenities = NestedField(AmenitySerializer, many=True)
    class Meta:
        model = Property
        fields = (
            'id', 'price', 'location', 'amenities'
        )

POST /api/property/

Request Body

{
    "price": 60000,
    "location": {
        "city": "Newyork",
        "country": "USA"
    },
    "amenities": {
        "add": [3],
        "create": [
            {"name": "Watererr"},
            {"name": "Electricity"}
        ]
    }
}

What's done here is pretty clear, location will be created and associated with the property created, also create operation on amenities will create amenities with values specified in a list and associate with the property, add operation will add amenity with id 4 to a list of amenities of the property.

Note: POST for many related field supports two operations which are create and add.


Response

{
    "id": 2,
    "price": 60000,
    "location": {
        "id": 3,
        "city": "Newyork",
        "country": "USA"
    },
    "amenities": [
        {"id": 1, "name": "Watererr"},
        {"id": 2, "name": "Electricity"},
        {"id": 3, "name": "Swimming Pool"}
    ]
}

PUT /api/property/2/

Request Body

{
    "price": 50000,
    "location": {
        "city": "Newyork",
        "country": "USA"
    },
    "amenities": {
        "add": [4],
        "create": [{"name": "Fance"}],
        "remove": [3],
        "update": {1: {"name": "Water"}}
    }
}

Note: Here add, create, remove and update are operations, so add operation add amenitiy with id 4 to a list of amenities of the property, create operation create amenities with values specified in a list, remove operation dessociate amenities with id 3 from a property, update operation edit amenity with id 1 according to values specified.

Note: PUT/PATCH for many related field supports four operations which are create, add, remove and update.


Response

{
    "id": 2,
    "price": 50000,
    "location": {
        "id": 3,
        "city": "Newyork",
        "country": "USA"
    },
    "amenities": [
        {"id": 1, "name": "Water"},
        {"id": 2, "name": "Electricity"},
        {"id": 4, "name": "Bathtub"},
        {"id": 5, "name": "Fance"}
    ]
}


Using NestedField with accept_pk=True kwarg.

accept_pk=True is used if you want to update nested field by using pk/id of existing data(basically associate and dessociate existing nested resources with the parent resource without actually mutating the nested resource). This applies to ForeignKey relation only.

from app.models import Location, Amenity, Property
from drf_pretty_update.serializers import NestedModelSerializer 
from drf_pretty_update.fields import NestedField


class LocationSerializer(NestedModelSerializer):
    class Meta:
        model = Location
        fields = ("id", "city", "country")


class PropertySerializer(NestedModelSerializer):
    location = NestedField(ocationSerializer, accept_pk=True)
    class Meta:
        model = Property
        fields = (
            'id', 'price', 'location'
        )

POST /api/property/

Request Body

{
    "price": 40000,
    "location": 2
}

Note: Here location resource with id 2 is already existing, so what's done here is create new property resource and associate it with a location with id 2.

Response

{
    "id": 1,
    "price": 40000,
    "location": {
        "id": 2,
        "city": "Tokyo",
        "country": "China"
    }
}

Using NestedField with create_ops=[..] and update_ops=[..] kwargs.

You can restrict some operations by using create_ops and update_ops keyword arguments as follows

from app.models import Location, Amenity, Property
from drf_pretty_update.serializers import NestedModelSerializer 
from drf_pretty_update.fields import NestedField


class AmenitySerializer(NestedModelSerializer):
    class Meta:
        model = Amenity
        fields = ("id", "name")


class PropertySerializer(NestedModelSerializer):
    amenities = NestedField(
        AmenitySerializer, 
        many=True,
        create_ops=["add"],  # Allow only add operation(restrict create operation)
        update_ops=["add", "remove"]  # Allow only add and remove operations(restrict create and update operations)
    )
    class Meta:
        model = Property
        fields = (
            'id', 'price', 'amenities'
        )

POST /api/property/

Request Body

{
    "price": 60000,
    "amenities": {
        "add": [1, 2]
    }
}

Note: According to create_ops=["add"], you can't use create operation in here!.

Response

{
    "id": 2,
    "price": 60000,
    "amenities": [
        {"id": 1, "name": "Watererr"},
        {"id": 2, "name": "Electricity"}
    ]
}

PUT /api/property/2/

Request Body

{
    "price": 50000,
    "amenities": {
        "add": [3],
        "remove": [2]
    }
}

Note: According to update_ops=["add", "remove"], you can't use create or update operation in here!.

Response

{
    "id": 2,
    "price": 50000,
    "amenities": [
        {"id": 1, "name": "Water"},
        {"id": 3, "name": "Bathtub"}
    ]
}

Running Tests

python setup.py test

Project details


Download files

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

Files for drf-pretty-update, version 0.1.5
Filename, size File type Python version Upload date Hashes
Filename, size drf_pretty_update-0.1.5-py3-none-any.whl (8.7 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size drf-pretty-update-0.1.5.tar.gz (7.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page