I am pulling my hair out with this as I haven't had any issues with flask or WTForms and have built quite a large application so far. Here is what I am trying to do.
I have 2 data models, Subscription, and Billing interval. There is a foreign key between the two and everything works well:
class Subscriptions(db.Model):
__tablename__ = 'subscriptions'
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(64))
description = db.Column(db.String(255))
lastcrawled = db.Column(db.Date)
lastcheckedby = db.Column(db.Integer)
isActive = db.Column(db.Boolean, default = False)
datecreated = db.Column(db.Date, default = datetime.date.today())
pricing = db.Column(db.Numeric(10, 2), nullable=True)
alias = db.Column(db.String(64))
isQuote = db.Column(db.Boolean, default = True)
billinginterval_id = db.Column(db.Integer, db.ForeignKey('billinginterval.id'))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class BillingInterval(db.Model):
__tablename__ = 'billinginterval'
id = db.Column(db.Integer, primary_key = True)
interval = db.Column(db.String(64))
subscriptions = db.relationship('Subscriptions', backref='billinginterval')
def __str__(self):
return self.interval
I have a form where a user can add another subscription:
class AddSubscription(FlaskForm):
name = StringField("Name", validators=[DataRequired()])
description = TextAreaField('Description')
pricing = IntegerField('Pricing')
billinginterval = SelectField('Billing Interval',choices=BillingInterval.query.all())
submit = SubmitField("Add Subscription")
And finally here is my Template:
{% extends "layout.html" %}
{% block content %}
<form method="POST">
{# This hidden_tag is a CSRF security feature. #}
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name() }}<br>
{{ form.description.label }} {{ form.description() }}<br>
{{ form.pricing.label }} {{ form.pricing() }}<br>
{{ form.billinginterval.label }} {{ form.billinginterval() }}<br>
{{ form.submit() }}
</form>
{% endblock %}
I need to have a "SelectField" that populates from the "billinginterval" table (displays interval as the choices) and then writes the billinginterval.id field to the subscription model.
Everything works flawlessly until I try adding this billing interval dropdown. Just can't get it.
I've tried everything, I cannot get it to work. Any help would be grateful. Thanks!