Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Djngo REST API: KeyError at /api/update #83

Open
roopeshm456 opened this issue Nov 7, 2019 · 0 comments
Open

Djngo REST API: KeyError at /api/update #83

roopeshm456 opened this issue Nov 7, 2019 · 0 comments

Comments

@roopeshm456
Copy link

i was trying to perform bulk update using django-bulk-update

**https://github.com/aykut/django-bulk-update**

But it throws KeyError at /api/update 'id'

views

class CartUpdatesView(ListBulkCreateUpdateDestroyAPIView):
    queryset=models.Cart.objects.all()
    serializer_class=serializers.CartUpdatesSerializer

serializers

class CartUpdatesSerializer(BulkSerializerMixin,serializers.ModelSerializer):
       
        class Meta(object):
             model  = models.Cart 
             fields = '__all__'
             list_serializer_class=BulkListSerializer

models

class Cart(models.Model): 
    cart_id=models.AutoField(primary_key=True)
    product_qty=models.IntegerField()
    customer_id=models.IntegerField()    
 product_id=models.ForeignKey('Products',db_column='product_id',on_delete=models.CASCADE)

Does this happens because of 2 primary keys while joiing tables?. I dont have any idea on bulk update. By searching I figure out official doc provide only very little information on bulk update and everyone refers to use this package.

GET

HTTP 200 OK
Allow: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "cart_id": 1,
        "product_qty": 4,
        "customer_id": 1,
        "product_id": 1
    }
]

PUT

[
    {
        "cart_id": 1,
        "product_qty": 9,
        "customer_id": 1,
        "product_id": 1
    }
]

Traceback

Environment:


Request Method: PUT
Request URL: http://localhost:8000/api/update

Django Version: 2.2.5
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'shoppingcart',
 'rest_framework',
 'rest_framework.authtoken']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py" in dispatch
  505.             response = self.handle_exception(exc)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py" in handle_exception
  465.             self.raise_uncaught_exception(exc)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py" in raise_uncaught_exception
  476.         raise exc

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\views.py" in dispatch
  502.             response = handler(request, *args, **kwargs)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework_bulk\generics.py" in put
  140.         return self.bulk_update(request, *args, **kwargs)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework_bulk\drf3\mixins.py" in bulk_update
  73.         serializer.is_valid(raise_exception=True)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py" in is_valid
  737.                 self._validated_data = self.run_validation(self.initial_data)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py" in run_validation
  618.         value = self.to_internal_value(data)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py" in to_internal_value
  654.                 validated = self.child.run_validation(item)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\serializers.py" in run_validation
  430.         value = self.to_internal_value(data)

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework_bulk\drf3\serializers.py" in to_internal_value
  27.             id_field = self.fields[id_attr]

File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\utils\serializer_helpers.py" in __getitem__
  148.         return self.fields[key]

Exception Type: KeyError at /api/update
Exception Value: 'id'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant