I am trying to create a serializer to aggregate some data regarding a users inventory, however it is throwing the following error:
Exception Value:
int() argument must be a string, a bytes-like object or a number, not 'dict'
I am not sure what time missing here. I am trying to sum the fields of standard and foil for a given user.
Model.py
class inventory_tokens(models.Model):
...
standard = models.IntegerField(default=0)
foil = models.IntegerField(default=0)
serializers.py
class InventorySerializers(serializers.Serializer):
total_Cards_Unique = serializers.IntegerField()
total_Cards = serializers.IntegerField()
total_Standard = serializers.IntegerField()
total_Foil = serializers.IntegerField()
views.py
# Inventory Data
class InventoryData(views.APIView):
def get(self, request):
user_id = self.request.user
data = [{
"total_Cards_Unique": inventory_cards.objects.filter(user_id=user_id).count(),
"total_Cards": 0,
"total_Standard": inventory_cards.objects.filter(user_id=user_id).aggregate(Sum('standard')),
'total_Foil': inventory_cards.objects.filter(user_id=user_id).aggregate(Sum('foil')),
}]
results = InventorySerializers(data, many=True).data
return Response(results)
full traceback
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/account/inventory/inventory-data/
Django Version: 3.2.7
Python Version: 3.7.9
Installed Applications:
['base.apps.BaseConfig',
'account.apps.AccountConfig',
'administration.apps.AdministrationConfig',
'magic.apps.MagicConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'psycopg2',
'background_task',
'rest_framework']
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 (most recent call last):
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "W:\My Drive\Projects\Card Companion\card_companion\website\account\views.py", line 42, in get
results = InventorySerializers(data, many=True).data
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\serializers.py", line 745, in data
ret = super().data
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\serializers.py", line 246, in data
self._data = self.to_representation(self.instance)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\serializers.py", line 664, in to_representation
self.child.to_representation(item) for item in iterable
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\serializers.py", line 664, in <listcomp>
self.child.to_representation(item) for item in iterable
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\serializers.py", line 515, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "W:\My Drive\Projects\Card Companion\card_companion\card_companion_venv\lib\site-packages\rest_framework\fields.py", line 963, in to_representation
return int(value)
Exception Type: TypeError at /account/inventory/inventory-data/
Exception Value: int() argument must be a string, a bytes-like object or a number, not 'dict'
CodePudding user response:
For the Sum
s you will need to "unpack" the data in the dictionary, so:
qsresult = inventory_cards.objects.filter(
user_id=user_id
).aggregate(
standard_sum=Sum('standard'),
foil_sum=Sum('foil'),
count=Count('pk')
)
data = [{
'total_Cards_Unique': qsresult['count'],
'total_Cards': 0,
'total_Standard': qsresult['standard_sum'],
'total_Foil': qsresult['foil_sum']
}]
By making multiple aggregates with the same QuerySet
, we thus limit the number of queries to the database to one.