Hi Brendan,
Better late than never... Wagtail 1.8 adds the ability to pass a callable as the `choices` parameter to a ChoiceBlock, so you'll be able to do this:
def get_school_choices():
return [(
school.id,
school.name) for school in School.objects.all()]
then in the StreamField definition:
ChoiceBlock(choices=get_school_choices)
Note that currently this only works when `choices` is passed to the ChoiceBlock constructor, not as a property of a subclass, so you can't write:
class CourseListBlock(ChoiceBlock):
choices = get_school_choices
For the second half of the problem, returning a list of course URLs, I suspect you don't really want to return that from value_from_form - that would mean that the list of course URLs is what gets stored in the database (and you'd have to somehow turn that back into a school ID when re-displaying the edit form). Instead, we probably want the stored value to be the school ID (which is what the get_school_choices code above does), and to perform the lookup of courses at the point of rendering the block on the page. This is best done with a `get_context` method:
def get_school_choices():
return [(
school.id,
school.name) for school in School.objects.all()]
class CourseListBlock(ChoiceBlock):
def get_context(self, value):
context = super(CourseListBlock, self).get_context(value)
context['award_urls'] = [award.get_absolute_url() for award in Award.objects.filter(school__id=value)]
class Meta:
template = 'myapp/blocks/course_list.html'
# this template will be used when you invoke {% include_block %} on the page template;
# you can refer to the 'award_urls' variable here
# in the StreamField definition:
CourseListBlock(choices=get_school_choices)
Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
wagtail+u...@googlegroups.com.
> To post to this group, send email to
wag...@googlegroups.com.
> Visit this group at
https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit
https://groups.google.com/d/msgid/wagtail/91f236de-d5c6-483b-8d00-03da5e701898%40googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.