Issue
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
}
Solution
Just a small change in the code mentioned by nstvnsn, Flask-RESTPlus is no longer being maintained.
So, instead, we can use flask_restx.
from flask_restx import inputs
parser.add_argument("OnlineAvailability", required=False, type=inputs.boolean)
Answered By - Rohit Nidimukkala
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.