'How to check data of POST/PUT api method in Flask
data = request.get_json(force=True)
print(data)
emp = Employee.query.get(employee_id_to_update)
print(emp.name)
print(set(data.keys()))
if 'dm' in data.keys() and logged_user.designation.lower() != "hr":
return jsonify("You cannot perform this action")
if data.get('name') :
emp.name=data\['name'\]
if data.get('email'):
emp.name = data\['email'\]
if data.get('gender'):
emp.name = data\['gender'\]
I have tried this but i dont want multiple if statements , is there any way to replace if else statements
Solution 1:[1]
Are you looking for this? This is shorter, but I don't think this is any more readable:
for field in ('name','email','gender'):
if field in data:
setattr( emp, field, data[field] )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
