Bot问题。在发送数据时,bot给出与数据库中缺少用户匹配相关的错误。在这种情况下,用户实际上没有被添加到proceed_request()
函数中的基中。有什么问题吗?谢谢。
def proceed_request(self, viber_request):
if isinstance(viber_request, ViberMessageRequest):
if not User.objects.filter(user_id=viber_request.sender.id).exists():
User.objects.update_or_create(
user_id=viber_request.sender.id,
name=viber_request.sender.name,
country=viber_request.sender.country,
language=viber_request.sender.language
)
def send_confirmation(self, confirmation):
order = Order.objects.create(
user_id=User.objects.get(user_id=data.get('viber_id')).id,
order_id=data.get('order_id'),
)
检查if not User.objects.filter(...).exists()
是多余的,因为您已经使用update_or_create
。在文档中,您可以找到您想要的defaults
参数:
def proceed_request(self, viber_request):
if isinstance(viber_request, ViberMessageRequest):
User.objects.update_or_create(
user_id=viber_request.sender.id,
defaults=dict(
name=viber_request.sender.name,
country=viber_request.sender.country,
language=viber_request.sender.language
)
)
def send_confirmation(self, confirmation):
order = Order.objects.create(
user_id=User.objects.get(user_id=data.get('viber_id')).id,
order_id=data.get('order_id'),
)