Skip to content

insung151/drf-nested-serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

DRF Nested Serializer

Nested Serializer support for Django Rest Framework.

Installation

pip install drf-nested-serializer

Example

class Goods(models.Model):
    name = models.CharField(max_length=10)
    category = models.CharField(max_length=20)

class GoodsImage(models.Model):
    goods = models.ForeignKey(
        Goods,
        related_name='goods_images',
        on_delete=models.CASCADE
    )
    image_key = models.CharField(max_length=10)
    
    

class GoodsSerializer(NestedModelSerializer):

    class Meta:
        model = Goods
        fields = '__all__'
        nested_fields = {'goods_images': 'goods'} # {related_name: field_name}

The above will allow to create the following queries

POST
{
    "name": "string",
    "category: "string",
    "goods_images": [
        {"image_key": "string"},
        {"image_key": "string"},
        {"image_key": "string"}
    ]
}

About

Nested Serializer support for Django Rest Framework.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages