0

I'm new to Flask/SQLAlchemy. I have a database with two tables: Family and Chart. The Family table has two columns: id and familyname. The Chart table has several columns including industry. I was able to display the dropdown for Family/family field, but not the dropdown for Chart/industry field. Here is my code and would appreciate the help.

forms.py

from flask import request
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import ValidationError, DataRequired, Length
from app.models import Family, Chart

def family_query():
    return Family.query.order_by('familyname asc').all()
def industry_query():
    return Chart.query(Chart.industry.distinct()).all()
class FamilyForm(FlaskForm):
    optsFamily = QuerySelectField(query_factory=family_query,allow_blank=True, get_label='familyname',  blank_text="Click to select")
    optsIndustry = QuerySelectField(query_factory=industry_query,allow_blank=True, get_label='industry',  blank_text="Click to select")

routes.py

from flask import render_template, flash, redirect, url_for, request, g,current_app
from app import db
from app.main.forms import FamilyForm
from app.models import Family, Chart
from app.main import bp

@bp.route('/', methods=['GET', 'POST'])
def index():
    form = FamilyForm()
    return render_template('index.html', form = form )

HTML

    {{ form.csrf_token }}
    {{ form.optsFamily }}
     <ul>
      {% for error in form.optsFamily.errors %}
      <li style="color:red;">{{ error }}</li>
      {% endfor %}
   </ul>
    {{ form.optsIndustry }}  
    <ul>
      {% for error in form.optsIndustry.errors %}
      <li style="color:red;">{{ error }}</li>
      {% endfor %}
   </ul>
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
mgreen
  • 39
  • 9
  • There should've been an error and you should always include errors with their tracebacks in debugging questions. – Ilja Everilä Nov 24 '18 at 07:20
  • Possible duplicate of [Querying with function on Flask-SQLAlchemy model gives BaseQuery object is not callable error](https://stackoverflow.com/questions/40918479/querying-with-function-on-flask-sqlalchemy-model-gives-basequery-object-is-not-c) – Ilja Everilä Nov 24 '18 at 07:22
  • The error I got was related to app being aborted but didn't get a message explaining the reason. – mgreen Nov 24 '18 at 13:48

1 Answers1

0

got it. I updated the query as folllows:

def industry_query():
    return Chart.query.order_by('industry asc').distinct('industry').all()
mgreen
  • 39
  • 9