2

My code is as below

# Cafe TABLE Configuration
class Cafe(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(250), unique=True, nullable=False)
    map_url = db.Column(db.String(500), nullable=False)
    img_url = db.Column(db.String(500), nullable=False)
    location = db.Column(db.String(250), nullable=False)
    seats = db.Column(db.String(250), nullable=False)
    has_toilet = db.Column(db.Boolean, nullable=False)
    has_wifi = db.Column(db.Boolean, nullable=False)
    has_sockets = db.Column(db.Boolean, nullable=False)
    can_take_calls = db.Column(db.Boolean, nullable=False)
    coffee_price = db.Column(db.String(250), nullable=True)

HTTP POST - Create Record

app.route('/add_cafe', methods=['POST', 'GET'])


def add_cafe():
    new_data = Cafe(name=request.form.get("name"),
                    map_url=request.form.get("map_url"),
                    img_url=request.form.get("img_url"),
                    location=request.form.get("location"),
                    has_sockets=bool(int(request.form.get("has_sockets"))),
                    has_toilet=bool(int(request.form.get("has_toilet"))),
                    has_wifi=bool(int(request.form.get("has_wifi"))),
                    can_take_calls=bool(int(request.form.get("can_take_calls"))),
                    seats=request.form.get("seats"),
                    coffee_price=request.form.get("coffee_price")),

    new_cafe_json = jsonify(cafe=new_data)
    db.session.add(new_cafe_json)
    db.session.commit()
    return jsonify(result={"Success": "Added successfully"})

Screen shot of postman request and response

I am getting 404 error - not found. Other requests are working fine. Any help. I am a beginner.

1 Answer 1

2

It seems you arent decorating the view function correctly.

your code is like this:

app.route('/add_cafe', methods=['POST', 'GET'])


def add_cafe():
    new_data = Cafe(name=request.form.get("name"),
    map_url=request.form.get("map_url"),
    .....

And it should be like:

@app.route('/add_cafe', methods=['POST', 'GET'])
def add_cafe():
    your code...

It seems that your usage of SQLAlchemy isnt in good shape as well. In the part you add information to db.session you should add a Model Instance of the object you want to save, in your code the object is the variable new_data like this: db.session.add(new_data) and then commit.

Greetings!

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you so much for responding. I made suggested changes (how i missed that @). I still get the following error.
sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.NoneType' is not mapped
Traceback (most recent call last): File "C:\Users\LIC\PycharmProjects\RestAPI\venv\Lib\site-packages\sqlalchemy\orm\session.py", line 2566, in add state = attributes.instance_state(instance) AttributeError: 'tuple' object has no attribute '_sa_instance_state'
The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\LIC\PycharmProjects\RestAPI\venv\Lib\site-packages\flask\app.py", line 2088, in call return self.wsgi_app(environ, start_response) File "C:\Users\LIC\PycharmProjects\RestAPI\venv\Lib\site-packages\flask\app.py", line 2073, in wsgi_app response = self.handle_exception(e) File "C:\Users\LIC\PycharmProjects\RestAPI\venv\Lib\site-packages\flask\app.py", line 2070, in wsgi_app response = self.full_dispatch_request()
File "C:\Users\LIC\PycharmProjects\RestAPI\venv\Lib\site-packages\flask\app.py", line 1515, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\LIC\PycharmProjects\RestAPI\venv\Lib\site-packages\flask\app.py", line 1513, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\LIC\PycharmProjects\RestAPI\venv\Lib\site-packages\flask\app.py", line 1499, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.