April 2018
Beginner to intermediate
440 pages
11h 36m
English
For the District and County data tables, a final wrinkle is added, querying against the newly added state data to find the associated state by FIPS code. By querying the State class using session.query and filtering the state's data using the filter_by method (passing the FIPS code from the district records as the filter argument), and then specifying that the first result should be used, the correct state can be called. The variable state's id field is used to populate the district's state_id column to create the relationship:
# This uses the STFIPS data to query the State table and find the statefor count, record in enumerate(district_records): district = District() district.district = record[0] district.name = record[1]