skip to Main Content

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:

http://127.0.0.1:8000/online

{
    "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:

http://127.0.0.1:8000/online/16

{
    "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:

http://127.0.0.1:8000/online

{
    "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:

http://127.0.0.1:8000/online/16

{
    "OnlineScheduleId": 16,
    "OnlineTimeSlot": "18:00",
    "OnlineAvailability": false
}

2

Answers


  1. Chosen as BEST ANSWER

    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)
    

  2. 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.

    
    from flask_restplus import inputs
    
    parser.add_argument('OnlineAvailability', required=False, type=inputs.boolean)
    
    

    To confirm this, I hacked together an app to reproduce the problem by using the code you provided:

    cURL POST in Terminal

    cURL POST Error

    I added the type argument to the parser.add_argument function call and the same post request through cURL works:

    cURL POST Success

    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):

    Warning

    The whole request parser part of Flask-RESTPlus is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.

    https://flask-restplus.readthedocs.io/en/stable/parsing.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search