I want to get the Default value of OnlineAvailability as True. By default, all Doctors will have all Slot Timings Available.
If I give the JSON Body data in Postman and POST the data:
{
"OnlineTimeSlot": "18:30"
}
Here, OnlineAvailability is being set to true
by default and I get the message:
{
"message": "New Time Slot, 18:30 added!"
}
When I GET the data, it shows:
{
"OnlineScheduleId": 16,
"OnlineTimeSlot": "18:30",
"OnlineAvailability": true
}
But,
If I want to give the OnlineAvailability as false
and POST the data or If I want to update the existing Timings Data using the PUT method in Postman JSON body:
{
"OnlineTimeSlot": "18:30",
"OnlineAvailability": false
}
Then, I am getting the error:
sqlalchemy.exc.StatementError: (builtins.TypeError) Not a boolean value: ‘False’
[SQL: INSERT INTO "OnlineSchedules" ("OnlineTimeSlot", "OnlineAvailability") VALUES (?, ?)] [parameters: [{‘OnlineTimeSlot’: ’18:30′, ‘OnlineAvailability’: ‘False’}]] // Werkzeug Debugger
How do I change the default value from true
to false
or upload a new Time Slot with OnlineAvailability as false without getting the above error? (The value should be recognised as a Boolean Value instead of a String)
online.py –> models
# omitted code
class OnlineScheduleModel(db.Model):
# omitted code
OnlineTimeSlot = db.Column(db.String(500), unique=True, nullable=False)
OnlineAvailability = db.Column(db.Boolean, nullable=False, default=True, server_default="true")
def __init__(self, OnlineTimeSlot, OnlineAvailability):
self.OnlineTimeSlot = OnlineTimeSlot
self.OnlineAvailability = OnlineAvailability
def json(self):
return {"OnlineScheduleId": self.OnlineScheduleId, "OnlineTimeSlot": self.OnlineTimeSlot, "OnlineAvailability": self.OnlineAvailability}
# ommitted code
online.py –> resources
# omitted code
class OnlineScheduleInfo(Resource):
parser = reqparse.RequestParser()
parser.add_argument("OnlineTimeSlot", required=True)
parser.add_argument("OnlineAvailability", required=False)
# omitted code
@cross_origin(supports_credentials=True)
def post(self):
data = OnlineScheduleInfo.parser.parse_args()
schedule = OnlineScheduleModel(**data)
if OnlineScheduleModel.find_by_timeslot(data['OnlineTimeSlot']):
return {"message": "A timeslot '{}' already exists".format(data['OnlineTimeSlot'])}, 400
# omitted code
schedule.save_to_db()
# omitted code
return {"message": "New Time Slot, {} added!".format(data['OnlineTimeSlot'])}, 200
@cross_origin(supports_credentials=True)
def put(self):
data = OnlineScheduleInfo.parser.parse_args()
schedule = OnlineScheduleModel.find_by_timeslot(data['OnlineTimeSlot'])
if schedule is None:
schedule = OnlineScheduleModel(**data)
else:
schedule.OnlineAvailability = data["OnlineAvailability"]
schedule.save_to_db()
return {"message": "schedule, {} Updated!".format(data['OnlineTimeSlot'])}, 200
Basically, My requirement is that I should be able to POST or PUT data with OnlineAvailability as false
, and I should get the output in Postman for GET:
{
"OnlineScheduleId": 16,
"OnlineTimeSlot": "18:00",
"OnlineAvailability": false
}
2
Answers
Just a small change in the code mentioned by nstvnsn, Flask-RESTPlus is no longer being maintained.
So, instead, we can use flask_restx.
When you are adding an argument to the parser, you can leave out the type argument for simple primitive types. For boolean, you may need to declare it for wider boolean handling. Different databases handle boolean values differently.
If you look at the docs for sqlite3, which is the engine your app is using, it stores boolean values as 0 for false and 1 for true. The parser needs to know that the added argument is a boolean so it can parse the python native boolean type.
To confirm this, I hacked together an app to reproduce the problem by using the code you provided:
I added the type argument to the
parser.add_argument
function call and the same post request through cURL works:Also, looking through the docs for Flask_restplus.reqparse.RequestParser (which is considered deprecated past 2.0, their docs mention it being replaced with a better alternative like marshmallow):
https://flask-restplus.readthedocs.io/en/stable/parsing.html