简体   繁体   中英

How do I get a time interval from a flask wtform?

I want the user to be able to enter something like '8 minutes 30 seconds' or even '8:30' into a form. Then I want to be able to store that in a PostgreSQL database as an INTERVAL data type.

It looks like you can take the difference between two DateTimes, but I was wondering if it is possible for the user to just enter in a time interval manually?

class Run(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    distance = db.Column(db.Float)
    time = db.Column(db.Interval)

class RunForm(FlaskForm):
    distance = FloatField('Distance', validators=[DataRequired()])
    time = ???

A clean way to do this is to store the data as an integer representing the interval in seconds or minutes. You do the math to store the interval on save, and then when you need to display it, you convert the stored integer to the appropriate format. This is equivalent to the way you generally always store dates at UTC, and then display them in the appropriate time zone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM