I have a file db_table.py that looks like: When I try to run: I get: I tried first manually setting FLASK_APP var, by doing set FLASK_APP=app.py then running flask db init again, but that didn’t resolve the issue. Answer The flask command line argument needs to know what module to locate the current Flask app instance in. Set FLASK_APP as
Tag: flask-sqlalchemy
Passing a row ID in a table to a modal in flask (python)
I have an HTML table which displays some data from a database table.Data is displayed by using a loop.Each row has a button that is also generated via a loop. Am using the button to edit data in a row. When a button is clicked, a modal pops up with data to be edited.The problem is that it only picks
has no attribute validate_on_submit: How to use wtforms-alchemy’s ModelForm in a flask handler / view
I’m trying to switch from wtforms.ext.sqlalchemy.orm import model_form to using wtforms_alchemy ModelForm: I’m failing miserably… and I’ve been unsuccessful searching for a working example of that uses wtforms_alchemy which shows both the handler and the model. Using model_form works for me: Here’s my working code using model forms: The model script (python): The handler script (python, flask, model_form): The template
flask-admin form: Constrain Value of Field 2 depending on Value of Field 1
One feature I have been struggling to implement in flask-admin is when the user edits a form, to constrain the value of Field 2 once Field 1 has been set. Let me give a simplified example in words (the actual use case is more convoluted). Then I will show a full gist that implements that example, minus the “constrain” feature.
Flask validates decorator multiple fields simultaneously
I have been using the @validates decorator in sqlalchemy.orm from flask to validate fields, and all has gone well as long as all of the fields are independent of one another such as: However, now I need to do some validation that will require access to field_one and field_two simultaneously. It looks like validates accepts multiple arguments to the validates
Show the SQL generated by Flask-SQLAlchemy
I want to get the SQL issued by Flask-SQLAlchemy queries. In Django, I can print the query attribute to get SQL. How can I get something similar in Flask? Answer Flask-SQLAlchemy records debugging information about all queries during a request. You can get the information with get_debug_queries(). Flask-Debugtoolbar, for example, uses this to offer a debug panel with query timing
Flask-Babel do not translate anything in a web project
Description of my usage: This is my project structure(basically base on Flask Web Development): Configuration in /babel.cfg and /app/__init__.py babel.cfg: app/__init__.py: Then I follow the Flask-Babel document Run $ pybabel extract -F babel.cfg -o messages.pot . Run $ pybabel extract -F babel.cfg -k lazy_gettext -o messages.pot . They do have found all the gettexts and lazy_gettexts. Run $ pybabel init
PostgreSQL ILIKE query with SQLAlchemy
I’d like to run a query that selects all posts, case insensitive, that have titles that match ‘%’ + [some_phrase] + ‘%’. That is, select all rows that have titles that contain some phrase, case insensitive. From the research I’ve done, it looks like I need to use Postgres’s ILIKE query for it to match case insensitive. How can I
Creating seed data in a flask-migrate or alembic migration
How can I insert some seed data in my first migration? If the migration is not the best place for this, then what is the best practice? Answer Alembic has, as one of its operation, bulk_insert(). The documentation gives the following example (with some fixes I’ve included): Note too that the alembic has an execute() operation, which is just like
How to execute raw SQL in Flask-SQLAlchemy app
How do you execute raw SQL in SQLAlchemy? I have a python web app that runs on flask and interfaces to the database through SQLAlchemy. I need a way to run the raw SQL. The query involves multiple table joins along with Inline views. I’ve tried: But I keep getting gateway errors. Answer Have you tried: or: Note that db.engine.execute()