Setting default for QuerySelectFields as part of FieldList with Flask-wtf

Viewed 99

In a flask-wtf form, I can't seem to set the default of a QuerySelectField when it's embbedded in a FieldList.

(I'm passing the instance in the database that I want to be the default, rather than the scalar.)

Code is as follows:-

Model:

class Jobs_crewing_roles(db.Model):
  __tablename__ = 'jobs_crewing_roles'
  sort_by = 'roles_description'
  roles_abbreviation = db.Column(db.String(1), primary_key=True, nullable=False)
  roles_description = db.Column(db.Text)
  roles_short_description = db.Column(db.String(10))
  roles_crewing = db.relationship('Jobs_crewing', backref='role')

Form:

def CrewingRoles():
  return lambda: Jobs_crewing_roles.query.all()

class CrewingRoleEntryForm(FlaskForm):
  role = QuerySelectField('Role', query_factory=CrewingRoles(), get_label = 'roles_description')

class ReviewFlightForm(FlaskForm):
  t_job_crewing_roles = FieldList(FormField(CrewingRoleEntryForm), min_entries=0)
  t_job_review_submit = SubmitField('Save Changes')

Helper function:

job.review_form = ReviewFlightForm()
roles_items = []
for p in ast.literal_eval(form.t_job_crewing_roles_dict.data):
  select_entry = CrewingRoleEntryForm()
  select_entry.role.id = "role_"+str(p['id'])
  select_entry.role.label = p['id']
  select_entry.role.default = Jobs_crewing_roles.query.filter(Jobs_crewing_roles.roles_abbreviation == p['role']).first()
  roles_items.append(select_entry)

job.review_form.t_jobs_crewing_roles = roles_items

(form.t_job_crewing_roles_dict.data is passed from a previous form, a stringified list of dictionaries in the form {'id': (person id), 'role': (role abbreviation in Jobs_crewing_roles table)})

The rest of the helper function is working as I get the correct id and label. But the QuerySelectField default isn't being set no matter what I try!

Edited to add: The Flask debugger seems to think it's been set correctly, but it's not being selected in the form HTML...?:

[console ready]
>>> review_form.t_jobs_crewing_roles[0].role.default
<Job Crewing Roles Record: Engineer>
>>> 
0 Answers
Related