Developer

 View Only
last person joined: 3 days ago 

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 = [
                models.UniqueConstraint(
                    fields=['bus_company_route', 'bus_type'],
                    name='unique_route_price'
                )
            ]
    

    and Here is how is serialized my data

    class AddRoutePriceSerializers(serializers.ModelSerializer):
        class Meta:
            model = RoutePrice
            fields = (
                'bus_type',
                'seat_price_for_travel_agent',
                'seat_price_for_user',
                'seat_price_for_foreigner',
            )
    
    
    class AddMultipleRoutePriceSerializers(serializers.Serializer):
        data = AddRoutePriceSerializers(many=True)
    

    I had my logic in usecases.py and views.py

    #usecases.py

    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):
            self._factory()
    
        def _factory(self):
            print(self._data)
            self.route_price = RoutePrice(**self._data, bus_company_route=self._bus_company_route)
            try:
                self.route_price.full_clean()
                self.route_price.save()
            except DjangoValidationError as e:
                raise ValidationError(e.message_dict)
    

    #views

    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(
                bus_company_route_id=self.kwargs.get(
                    'bus_company_route_id'
                )
            ).execute()
    
        def perform_create(self, serializer):
            return AddRoutePriceUseCases(serializer=serializer,
                                         bus_company_route=self.get_bus_company_route()
                                         ).execute()
    

    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
    Hello.

    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
    ------------------------------