You will want to have a look at the docs for more information, but Pewee supports joins out of the box:
class Course(db.Model):
name = CharField()
class Activity(db.Model):
# Note that we have added a backreference
# to activities on the `Course` model with
# the `related_name` argument
# so we can do course.activites
course = ForeignKeyField(Course, related_name='activities')
name = CharField()
results = Course.select(Course, Activity).join(Activity)
# results is now a collection of courses, each course
# with all of its activities and it all should have been
# pulled out with one SQL query
Then, in your Jinja template you can simply:
<select name="activities">
{% for course in courses %}
<optgroup label="course.name">
{% for activity in course.activities %}
<option value="{{activity.id}}">{{activity.name}}</option>
{% endfor %}
</optgroup>
{% endfor %}
</select>