简体   繁体   中英

User matching query does not exist. Viber bot Python

Bot problem. When sending data, the bot gives an error related to the absence of a user match in the database. In this case, indeed, the user is not added to the base in the proceed_request() function. What could be the problem? Thanks.

    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'),
        )

Check if not User.objects.filter(...).exists() is redundant as you already use update_or_create . In documentation you can find that there is defaults argument for what you want there:

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'),
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM