TypeError: orderTimelocation() got an unexpected keyword argument ‘order’

I have the models order and orderTimelocation. Order has a one to many relationship with orderTimelocation:

class Order(models.Model):

    customer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='customer')
    retailer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='retailer')
    date_publish = models.DateField()
    date_available = models.DateField()
    weight = models.DecimalField(decimal_places=2, max_digits=5)

class orderTimelocation(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='ordertimelocation'),
    longitude = models.DecimalField(decimal_places=8, max_digits=12)
    latitude = models.DecimalField(decimal_places=8, max_digits=12)
    

And trying to serialize the order model, that should receive ordertimelocation's. For that I'm using Writable nested serializers, as described on DRF documentation

class OrderSerializer(serializers.ModelSerializer):
    ordertimelocation = orderTimelocationSerializer(many=True)

    class Meta:
        model = Order
        fields = ['customer', 'retailer', 'date_publish', 'date_available', 'weight', 'ordertimelocation']

    def create(self, validated_data):
        timelocations_data = validated_data.pop('ordertimelocation')
        order = Order.objects.create(**validated_data)
        for timelocation_data in timelocations_data:
           orderTimelocation.objects.create(order=order, **timelocation_data)
        return order

I think my code is similar to the one on the documentation but the following erro happens:

Traceback (most recent call last):
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/generics.py", line 190, in post
    return self.create(request, *args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/mixins.py", line 19, in create
    self.perform_create(serializer)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/mixins.py", line 24, in perform_create
    serializer.save()
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/rest_framework/serializers.py", line 205, in save
    self.instance = self.create(validated_data)
  File "/home/miguel/workspace/projeto-final/backend/delivery_api/serializers/order_serializer.py", line 16, in create
    orderTimelocation.objects.create(order=ordera, **timelocation_data)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/db/models/query.py", line 451, in create
    obj = self.model(**kwargs)
  File "/home/miguel/workspace/projeto-final/backend/env/lib/python3.8/site-packages/django/db/models/base.py", line 503, in __init__
    raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: orderTimelocation() got an unexpected keyword argument 'order'


Read more here: https://stackoverflow.com/questions/68478276/typeerror-ordertimelocation-got-an-unexpected-keyword-argument-order

Content Attribution

This content was originally published by miguel__frtt at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: