beda-software / drf-writable-nested

Writable nested model serializer for Django REST Framework
Other
1.07k stars 116 forks source link

Add support for many=True on create #141

Closed feelixe closed 3 years ago

feelixe commented 3 years ago

This adds support for initializing serializer with many=True. Only with action create from view function. Needs to be reviewed. Example usage (Same as current usage, except if you want to use many=True, you have to instantiatethe serializer with that kwarg):

serializers.py

from drf_writable_nested import WritableNestedModelSerializer

class MyModelSerializer(WritableNestedModelSerializer):
    owner = PersonSerializer() # Some nested serializer
    toys = ToySerializer(many=True) # Another nested serializer

    class Meta:
        model = MyModel
        fields = '__all__'

views.py

from rest_framework import viewsets
from .serializers import MyModelSerializer
from .models import MyModel

class MyModelViewset(viewsets.ModelViewSet):
    serializer_class = MyModelSerializer
    queryset = MyModel.objects.all()

    def get_serializer(self, *args, **kwargs):
        if self.action == 'create':
            kwargs['many'] = True
        return super().get_serializer(*args, **kwargs)

Or to allow for sending one or many:

from rest_framework import viewsets
from .serializers import MyModelSerializer
from .models import MyModel

class MyModelViewset(viewsets.ModelViewSet):
    serializer_class = MyModelSerializer
    queryset = MyModel.objects.all()

    def get_serializer(self, *args, **kwargs):
        if self.action == 'create' and isinstance(kwargs['data'], list):
            kwargs['many'] = True
        return super().get_serializer(*args, **kwargs)
codecov-io commented 3 years ago

Codecov Report

Merging #141 (9096e78) into master (f7d470f) will decrease coverage by 3.29%. The diff coverage is 50.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #141      +/-   ##
==========================================
- Coverage   98.16%   94.87%   -3.30%     
==========================================
  Files           3        3              
  Lines         218      234      +16     
==========================================
+ Hits          214      222       +8     
- Misses          4       12       +8     
Impacted Files Coverage Δ
drf_writable_nested/mixins.py 94.59% <50.00%> (-3.47%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update f7d470f...9096e78. Read the comment docs.

auvipy commented 3 years ago

why you closed this?

feelixe commented 3 years ago

why you closed this?

I opened a new PR since I couldn't reopen this. Would you be able to assist in reviewing/testing my fork? I'm not sure I've done it in the best way.

auvipy commented 3 years ago

whenever you want to contribute work a on a new branch of your fork, not the master branch, then send the pr from your branch to this repo master branch