tombreit
tombreit

Reputation: 1379

Populate choice field from model

I'm aware that there is a Google Groups Wagtail support case with exactly the same title - but I don't get it.

I want to populate a Choice (?) on page A (class EventPage(Page)) with data from page B (class SpeakerPage(Page)); in order to let the user choose the (there could only be one) speaker for an event.

What I tried till now:

class SpeakerPage(Page):

    def __unicode__(self):
        return u'%s, %s' % (self.lastname, self.firstname)

    firstname = models.CharField(max_length=250)
    lastname = models.CharField(max_length=250)
    ...


class EventPage(Page):
    speaker = models.ForeignKey('SpeakerPage', 
            related_name='+', 
            null=True, blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('speaker'),
    ]

And I got a dropdown-menu on EventPage - but this menu is empty. How do I get the speakers in there?

Upvotes: 1

Views: 948

Answers (1)

tombreit
tombreit

Reputation: 1379

Sorry for the noise - the dropdown-menue for the ForeignKey is working as expected. Just the __unicode__ function did not achive the desired (formatted) result. A small modification did it:

def __str__(self): return '%s, %s' % (self.lastname, self.firstname)

Upvotes: 1

Related Questions