Try to search your question here, if you can't find : Ask Any Question Now ?

Django rest framework “`TypeError: type object argument after ** must be a mapping, not int“` being thrown

HomeCategory: stackoverflowDjango rest framework “`TypeError: type object argument after ** must be a mapping, not int“` being thrown
Avatarpragati asked 1 week ago

I have a SMSMessages model that will contain all the sms sent.I’m trying access via the django-rest-framework ListCreateAPIView class, but I keep on hitting error when I try to do an OPTION request on the api via Insomnia, TypeError at /smsmessages/
type object argument after ** must be a mapping, not int

I searched on SO and found similar errors but they were caused b/c of a filter() argument, which doesn’t apply to this.
I’m using django 2.2.5, python 3.6.8, django-rest-framework 3.10.3 and a mysqlclient 1.4.4.

this is the model of SMSMessages

from django.db import models
from django.contrib.auth.models import AbstractUser
from rest_framework.authtoken.models import Token

class SMSMessages(models.Model):
    sms_number_to = models.CharField(max_length=14)
    sms_content = models.CharField(max_length=160)
    sending_user = models.ForeignKey("SMSUser", on_delete=models.PROTECT, related_name="user_that_sent", limit_choices_to=1)
    sent_date = models.DateTimeField(auto_now=True)
    delivery_status = models.BooleanField(default=False)

    class Meta:
        verbose_name_plural = "SMSMessages"

    def __str__(self):
        return self.sending_user

and this is this the file:

from django.urls import path
from rest_framework.authtoken import views
from rest_framework.routers import DefaultRouter

from notification.apiviews import SMSendView, SMSMessagesView

app_name = "notification"

router = DefaultRouter()

urlpatterns = [
    path("smsmessages/", SMSMessagesView.as_view(), name="sms_messages"),

so the way I access it is by sending an OPTION request to http://localhost:8000/smsmessages/.
This the SMSMessagesview class that is used for the api view:

from rest_framework import generics, status, viewsets, permissions
from rest_framework.response import Response
from rest_framework.views import APIView
from django.shortcuts import get_object_or_404, get_list_or_404
from django.contrib.auth import authenticate

from commons.models import SMSMessages
from commons.serializers import SMSMessagesSerializer

class SMSMessagesView(generics.ListCreateAPIView):

    This class is responsible for generating, and returning, the view for all created objects of the SMSMessages model.
    It sub-classes the ListCreateAPIView class from the generics module.
    queryset = SMSMessages.objects.all()
    if not queryset:
        Response(data={"{0} not found".format(queryset)}, status=404, content_type="application/json")

    serializer_class = SMSMessagesSerializer

and this is the error:

TypeError at /notification/smsmessages/
type object argument after ** must be a mapping, not int

Request Method: OPTIONS
Request URL: http://localhost:8055/notification/smsmessages/
Django Version: 2.2.5
Python Executable: /home/gadd/vscodeworkspace/
Python Version: 3.6.8
Python Path: ['/home/gadd/vscodeworkspace/', '/home/gadd/vscodeworkspace/', '/home/gadd/vscodeworkspace/', '/home/gadd/vscodeworkspace/', '/usr/lib/python3.6', '/home/gadd/vscodeworkspace/']
Server time: Sat, 5 Oct 2019 16:19:59 +0000
Installed Applications:
Installed Middleware:


File "/home/gadd/vscodeworkspace/" in inner
  34.             response = get_response(request)

File "/home/gadd/vscodeworkspace/" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/gadd/vscodeworkspace/" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/gadd/vscodeworkspace/" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/home/gadd/vscodeworkspace/" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/home/gadd/vscodeworkspace/" in dispatch
  505.             response = self.handle_exception(exc)

File "/home/gadd/vscodeworkspace/" in handle_exception
  465.             self.raise_uncaught_exception(exc)

File "/home/gadd/vscodeworkspace/" in raise_uncaught_exception
  476.         raise exc

File "/home/gadd/vscodeworkspace/" in dispatch
  502.             response = handler(request, *args, **kwargs)

File "/home/gadd/vscodeworkspace/" in options
  516.         data = self.metadata_class().determine_metadata(request, self)

File "/home/gadd/vscodeworkspace/" in determine_metadata
  68.             actions = self.determine_actions(request, view)

File "/home/gadd/vscodeworkspace/" in determine_actions
  94.                 actions[method] = self.get_serializer_info(serializer)

File "/home/gadd/vscodeworkspace/" in get_serializer_info
  111.             for field_name, field in serializer.fields.items()

File "/home/gadd/vscodeworkspace/" in __get__
  80.         res = instance.__dict__[] = self.func(instance)

File "/home/gadd/vscodeworkspace/" in fields
  360.         for key, value in self.get_fields().items():

File "/home/gadd/vscodeworkspace/" in get_fields
  1047.                 source, info, model, depth

File "/home/gadd/vscodeworkspace/" in build_field
  1182.                 return self.build_relational_field(field_name, relation_info)

File "/home/gadd/vscodeworkspace/" in build_relational_field
  1257.         field_kwargs = get_relation_kwargs(field_name, relation_info)

File "/home/gadd/vscodeworkspace/" in get_relation_kwargs
  255.             limit_choices_to = models.Q(**limit_choices_to)

Exception Type: TypeError at /notification/smsmessages/
Exception Value: type object argument after ** must be a mapping, not int

This is the serializer class used:

from rest_framework import serializers
from rest_framework.authtoken.models import Token

from commons.models import SMSUser, SMSPrice, Type, SMSMessages

class SMSMessagesSerializer(serializers.ModelSerializer):
    A class for serializing the SMSMessages model's data. It sub-classes the
    ModelSerializer class from serializer's module.

    class Meta:
        model = SMSMessages
        fields = '__all__'

This same error is raised when I try to add an SMSMessages object to the db via the admin interface. Can anybody help?

1 Answers
Best Answer
AvatarAmit answered 1 week ago
Your Answer

17 + 13 =

Popular Tags

WP Facebook Auto Publish Powered By :