【问题标题】:Invalid input of type: 'Product'. Convert to a bytes, string, int or float first类型的无效输入:“产品”。先转换为字节、字符串、整数或浮点数
【发布时间】:2021-04-19 16:05:28
【问题描述】:

我使用的是 redis 6.2 版

这是我正在处理的视图:

elif request.method == 'POST':
    user_key = request.user
    bid_price = request.POST.get('bid')
    retrieve_product = request.POST.get('product.id')
    product_key = Product.objects.get(id=retrieve_product)
    minimum_price = product_key.base_price
    if float(bid_price) >= minimum_price:
        if redis_instance.hexists(product_key, "highest_bid"):
            hb_value = redis_instance.hget(product_key, "highest_bid")
        else:
            redis_instance.hset(product_key, "highest_bid", bid_price)
            hb_value = bid_price
        if bid_price > hb_value:
            redis_instance.hset(product_key, user_key, bid_price)  
            redis_instance.hset(product_key, "highest_bid", bid_price)
            messages.info('Your bid was successfully processed')
            return HttpResponse('successfully uploaded')
        else:
            return HttpResponse('Your bid is lower than current highest bid')

    else:
        return HttpResponse('Your bid is lower than minimum price')

'''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''

这就是模型:

class Product(models.Model):
    name = models.CharField(max_length=200, null=True)
    base_price = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
    digital = models.BooleanField(default=False, null=True, blank=True)
    start_date = models.DateTimeField(default=timezone.now)
    end_date = models.DateTimeField(default=one_day_hence)
    bidding_finished = models.BooleanField(default=False)

    def __str__(self):
        return self.name

错误回溯:

Traceback (most recent call last):   File
"C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 138, in run
    self.finish_response()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 196, in finish_response
    self.close()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py",
line 114, in close
    super().close()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\simple_server.py",
line 38, in close
    SimpleHandler.close(self)   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 335, in close
    self.result.close()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\http\response.py",
line 253, in close
    signals.request_finished.send(sender=self._handler_class)   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\dispatch\dispatcher.py",
line 173, in send
    return [   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\dispatch\dispatcher.py",
line 174, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\__init__.py",
line 57, in close_old_connections
    conn.close_if_unusable_or_obsolete()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\base.py",
line 525, in close_if_unusable_or_obsolete
    self.close()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\asyncio.py",
line 26, in inner
    return func(*args, **kwargs)   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\base.py",
line 261, in close
    if not self.is_in_memory_db():   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\base.py",
line 380, in is_in_memory_db
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])   File
"C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\creation.py",
line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name TypeError: argument of type 'WindowsPath' is not
iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File
"C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\socketserver.py",
line 650, in process_request_thread
    self.finish_request(request, client_address)   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\socketserver.py",
line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\socketserver.py",
line 720, in __init__
    self.handle()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py",
line 174, in handle
    self.handle_one_request()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py",
line 197, in handle_one_request
    handler.run(self.server.get_app())   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 145, in run
    self.handle_error()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\servers\basehttp.py",
line 119, in handle_error
    super().handle_error()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 382, in handle_error
    self.finish_response()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 184, in finish_response
    self.write(data)   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 288, in write
    self.send_headers()   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 345, in send_headers
    if not self.origin_server or self.client_is_modern():   File "C:\Users\alber\AppData\Local\Programs\Python\Python38\lib\wsgiref\handlers.py",
line 358, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9' TypeError: 'NoneType' object is not subscriptable

【问题讨论】:

  • 能否显示错误的回溯。
  • 添加了回溯
  • 这是另一个错误。

标签: python django redis


【解决方案1】:

根据您帖子标题中的错误消息(不是帖子中的回溯),您的问题是您正在使用 product_key 变量调用 Redis.hexists()Redis.hget()Redis.hset() 方法,其中包含Product 模型对象。

改为使用字符串值作为 Redis 键,例如 str(product_key.id)

您也需要为user_key 做类似的事情。

【讨论】:

  • 已经试过了,不行。给我同样的错误
  • @Non_specificato 这意味着您要么没有正确解决问题,要么在解决问题后出现第二个不同的错误。
猜你喜欢
  • 2023-01-18
  • 2020-06-10
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多