使用 if-elif-else 语句在回调函数中增加这些喜欢和检查模型属性的最佳方法是什么?

What is the best way to increment these likes and checks model attributes in a callback function using an if-elif-else statement?

提问人:Bobmaintain 提问时间:3/22/2023 最后编辑:Bobmaintain 更新时间:3/22/2023 访问量:42

问:

我不想创建两个回调函数。我希望使用 if-elif-else 语句以有条件的方式执行此回调。

首先,代码会按原样运行吗?那么,如果没有,将 house.likes 和 house.checks 放在 if-else 或 if-elif-else 语句中的最佳方式是什么?

def callback(ch, method, properties, body):
    print('Received in config')
    id = json.loads(body)
    print(id)
    
    house = House.objects.get(id=id)
    
    house.likes = house.likes + 1
    house.save()
    print('House likes increased!')
    
    house.checks = house.checks + 1
    house.save()
    print('House checks increased!')

我还没有尝试重构它......仍在研究代码块。我不想破坏东西。

python django 函数 django-models 回调

评论

0赞 Tanveer Ahmad 3/22/2023
house.likes += 1
0赞 Bobmaintain 3/22/2023
@Tanveer艾哈迈德,我知道我们可以写这些(>= 1、<=1、-= 1、+=1),但这不是我所追求的。我使用条件语句将属性 Likes 和 Checks 放在一个回调函数中。

答: 暂无答案