3

I am using DRF for my API backend. I need to insert multiple objects into one post request. I saw so many tutorials, as well How do I create multiple model instances with Django Rest Framework?, but not success. I am using ModelSerializer, but when using many=True then have problem with ListSerializer.

views.py

class SaleUserViewSet(ModelViewSet):
    queryset = SaleUser.objects.all()
    serializer_class = SaleUserSerializer(many=True)

serlializers.py

class SaleUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = SaleUser
        fields = ('id', 'comment', 'creation_date', 'modification_date', 'last_user', 'user', 'sale', 'user_sale_type')

error message

AttributeError at /api/sale_user/ type object 'Meta' has no attribute 'model'

Please advise.

2
  • Welcome to the stackoverflow. hi, add your code and error trace, please. Commented Aug 10, 2017 at 7:56
  • Thank you Bear, I edited my question with code and error message. Commented Aug 10, 2017 at 8:10

1 Answer 1

11

rollback serializer to your default

class SaleUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = SaleUser
        fields = (
            'id',
            'comment',
            'creation_date',
            'modification_date',
            'last_user',
            'user',
            'sale',
            'user_sale_type'
        )

and override view to it:

from rest_framework.response import Response    

class SaleUserViewSet(ModelViewSet):
    queryset = SaleUser.objects.all()
    serializer_class = SaleUserSerializer

    def create(self, request, *args, **kwargs):
        data = request.data.get('items', request.data)
        many = isinstance(data, list)
        print (data, many)
        serializer = self.get_serializer(data=data, many=many)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(
                serializer.data,
                status=status.HTTP_201_CREATED,
                headers=headers
        )
Sign up to request clarification or add additional context in comments.

10 Comments

I rewrite data = request.data.get("items") if 'items' in request.data else data = request.data to ` if 'items' in request.data: data = request.data.get("items") else: data = request.data` because I get this error SyntaxError: can't assign to conditional expression With rewrited code still get error AssertionError at /api/sale_user/ Cannot call .is_valid()` as no data= keyword argument was passed when instantiating the serializer instance.`
i'm sorry, i fixed both troubles you may try again
Ok, I tried again, now I have new error NameError at /api/sale_user/ name 'status' is not defined :(
it is good :) just add from rest_framework import status to your imports
@yaksh added the import to the answer, for other who will search the same
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.