last person joined: yesterday 

Expand all | Collapse all

Django: Got got an unexpected keyword argument after Nested serializer

This thread has been viewed 11 times
  • 1.  Django: Got got an unexpected keyword argument after Nested serializer

    Posted Jan 03, 2021 11:08 PM

    I have been Django working on RoutePrice Model and I tried to serialize data in nested serializer, before nested serializer it was fine working clean but I got type error after nested serializer. Here is Model and my serializer I have tried so far.

    class RoutePrice(BaseModel):
        bus_company_route = models.ForeignKey(BusCompanyRoute, on_delete=models.PROTECT)
        bus_type = models.ForeignKey(Category, on_delete=models.PROTECT)
        seat_price_for_travel_agent = AmountField()
        seat_price_for_user = AmountField()
        seat_price_for_foreigner = AmountField(null=True, blank=True)
     class Meta:
            default_permissions = ()
            verbose_name = 'Route Price'
            verbose_name_plural = 'Route Prices'
            constraints = [
                    fields=['bus_company_route', 'bus_type'],

    and Here is how is serialized my data

    class AddRoutePriceSerializers(serializers.ModelSerializer):
        class Meta:
            model = RoutePrice
            fields = (
    class AddMultipleRoutePriceSerializers(serializers.Serializer):
        data = AddRoutePriceSerializers(many=True)

    I had my logic in and

    class AddRoutePriceUseCases(BaseUseCase):
        def __init__(self,
                     serializer: AddMultipleRoutePriceSerializers,
                     bus_company_route: BusCompanyRoute):
            self._serializer = serializer
            self._data = serializer.validated_data
            self._bus_company_route = bus_company_route
        def execute(self):
        def _factory(self):
            self.route_price = RoutePrice(**self._data, bus_company_route=self._bus_company_route)
            except DjangoValidationError as e:
                raise ValidationError(e.message_dict)


    class AddRoutePriceView(generics.CreateAPIView):
        Use this end-point to add route price to specific bus company route
        serializer_class = route_price_serializers.AddMultipleRoutePriceSerializers
        def get_bus_company_route(self):
            return GetBusCompanyRouteUseCase(
        def perform_create(self, serializer):
            return AddRoutePriceUseCases(serializer=serializer,

    I got error saying Route

    RoutePrice() got an unexpected keyword argument 'data'

    what is wrong in nested serializer I think I made wrong there?

    john josef


  • 2.  RE: Django: Got got an unexpected keyword argument after Nested serializer

    Posted Jan 18, 2021 11:45 AM

    I'm not sure this is the best place for DRF chat, but to me it looks like the attribute `data` defined in your.serializer is being passed into your RoutePrice model which does not have a `data` attribute. That's what the error message says and since 

            self.route_price = RoutePrice(**self._data, bus_company_route=self._bus_company_route)
    is unpacking the serializer dict into RoutePrice, the data attribute is passed and you get the error.

    Conor Cunningham