Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Using SelectFields with flask-wtforms !HELP!

I'm having issues using Select Field in flask . I'm unable to submit get data from the select fields , also when i use them with normal input fields none of the fields return any data . But the input fields work and return data if I remove the selectFields . Here's the code :

Models and form :

class City(db.Model):
id = db.Column(db.Integer, primary\_key=True)
city = db.Column(db.String(30), unique=True, nullable=False)
state = db.Column(db.String(30), nullable=False)
country = db.Column(db.String(30), nullable=False)
class CityForm(FlaskForm):
city = StringField('city', validators=\[InputRequired()\])
state = SelectField('state' , validators=\[InputRequired()\] , coerce = str)
country = SelectField('country' , validators=\[InputRequired()\] , coerce = str)

the select fields are populated in the views , like this :

form\_city.state.choices = \[(row\[0\],row\[0\]) for row in db.session.query(login\_model.State.state)\]
form\_city.country.choices = \[(row\[0\],row\[0\]) for row in db.session.query(login\_model.Country.country)\]

\~\~\~\~\~

if form\_city.validate\_on\_submit():
mssg = ""
prod = login\_model.City.query.filter\_by(city=form\_city.city.data).first()
print('okaaay')
if prod :
mssg = "Duplicate Data "
flash(mssg)
return redirect(url\_for('basic\_master'))
else:
new\_data = login\_model.City(city=form\_city.city.data.upper())
try:
db.session.add(new\_data)
db.session.commit()
mssg = "Data Successfully added πŸ‘"
flash(mssg)
return redirect(url\_for('basic\_master'))

except Exception as e:
mssg = "Error occured while adding data 😡. Here's the error : "+str(e)
flash(mssg)
return redirect(url\_for('basic\_master'))
Nothing works if I have selectfields in my Form class , and If i remove them the normal input field works .

Any help is much appreciated thanks.

\#flask #sqlalchemy #wtforms #python

/r/flask
https://redd.it/8srquo
New Fask library to make uploading, storing & streaming files easy!

Hi guys

I have just finished work on a new Flask library to make uploading files and storing them on your server & database as easy as possible... just let Flask-File-Upload handle all the repetitive work!

Works with Flask & Flask-SQLAlchemy

[https://github.com/joegasewicz/Flask-File-Upload](https://github.com/joegasewicz/Flask-File-Upload)

​

I would be very grateful if anyone wishes to try out this library & or give some feed back. And of course contributions are welcome!

Thanks so much

Joe

​

https://preview.redd.it/ppd6fr092g941.jpg?width=2200&format=pjpg&auto=webp&s=9fe80a5d18d313158672805eddb42edbfd1de506

\#python #flask #sqlalchemy

/r/flask
https://redd.it/elk36d
Simple syntax question- how to drop one table in SQL Alchemy / Flask?


The Flask documentation seems to only discuss how to drop ALL the tables:
https://flask-sqlalchemy.palletsprojects.com/en/2.x/binds/#creating-and-dropping-tables

I don't know how to interpret this, https://docs.sqlalchemy.org/en/14/core/metadata.html#sqlalchemy.schema.Table.drop. I'm not sure if it would apply to working in Flask.

All the online answers regarding deleting one table in SQL Alchemy discuss it as engine, not, as one stackoverflow user says, a "Flask-SQLAlchemy extension object".



I need to drop a table every time the app runs because the app automatically populates this table with data. If it tries to populate a populated table, it will get a "UNIQUE" error.

On my init.py I have:
~~~
from flasksite.models import Styletable
db = SQLAlchemy(app)
db.dropall()
db.create
all()
Styletable.createtable()
~~~
This works great to populate the data. But, it drops all the data from all the tables. That's not good.

I wish I could just do something like

db.drop(Styletable)

or

db.Styletable.drop()

on my models.py I have:
~~~~
class Styletable(db.Model):
id = db.Column(db.Integer, primarykey=True)
style
name = db.Column(db.String(100), primarykey=True, nullable= False)

def createtable():
a = Styletable()
b = Styletable()
c = Styletable()
a.style
name = "essay"


/r/flask
https://redd.it/pqmj4g