简体   繁体   English

Django 总和抛出 `int() 参数必须是一个字符串,一个类似字节的 object 或一个数字,而不是'dict'`

[英]Django aggregate sum throwing `int() argument must be a string, a bytes-like object or a number, not 'dict'`

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 Model.py

class inventory_tokens(models.Model):
    ...
    standard    = models.IntegerField(default=0)
    foil        = models.IntegerField(default=0)

serializers.py序列化器.py

class InventorySerializers(serializers.Serializer):
    total_Cards_Unique = serializers.IntegerField()
    total_Cards = serializers.IntegerField()
    total_Standard = serializers.IntegerField()
    total_Foil = serializers.IntegerField()

views.py视图.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'

For the Sum s you will need to "unpack" the data in the dictionary, so:对于Sum s,您需要“解压”字典中的数据,因此:

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.通过使用相同的QuerySet进行多个聚合,我们因此将对数据库的查询数量限制为一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int() 参数必须是字符串、类似字节的 object 或数字,而不是 'dict' - int() argument must be a string, a bytes-like object or a number, not 'dict' python中的数据增强引发错误“ int()参数必须是字符串,类似字节的对象或数字,而不是&#39;dict&#39;” - Data augmentation in python throws an error “int() argument must be a string, a bytes-like object or a number, not 'dict'” int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - int() argument must be a string, a bytes-like object or a number, not 'tuple' int() 参数必须是字符串、类似字节的对象或数字,而不是 &#39;QueryDict&#39; - int() argument must be a string, a bytes-like object or a number, not 'QueryDict' int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType” - int() argument must be a string, a bytes-like object or a number, not 'NoneType' int() 参数必须是字符串、类似字节的对象或数字,而不是“QuerySet” - int() argument must be a string, a bytes-like object or a number, not 'QuerySet' Django int() 参数必须是字符串、类似字节的对象或数字,而不是“QueryDict” - Django int() argument must be a string, a bytes-like object or a number, not 'QueryDict' Django TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是&#39;list&#39; - Django TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' Django: TypeError: int() argument must be a string, a bytes-like object or a number, not 'Driver' - Django : TypeError: int() argument must be a string, a bytes-like object or a number, not 'Driver'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM