You can set the choices in the form __init__ to handle the issue:

 class FooJBossForm(Form):
    biz_service = MultipleChoiceField(
        choices=[],
        required=False,
        label='Business Service',
        widget=SelectMultiple(attrs={'class': 'form-control'}),
    )

    def __init__(self, *args, **kwargs):
        super(FooJBossForm, self).__init__(*args, **kwargs)
        try:
            self.fields['biz_service'].choices = BizService.objects.filter(
                                   biz_unit__bu_name='Foo',
                                                 ).order_by('bs_name')\
                                                  .values_list('bs_name', 
'bs_name')\
                                                  .distinct()
        except ProgrammingError:
            pass #OR Some placeholder choices

Kirby



On Thursday, February 15, 2018 at 9:56:56 AM UTC-5, [email protected] 
wrote:
>
> Hello everyone,
>
> I have a form field that is pulling choices from the database:
>
> class FooJBossForm(Form):
>     biz_service = MultipleChoiceField(
>         choices=BizService.objects.filter(
>             biz_unit__bu_name='Foo',
>         ).order_by('bs_name').values_list('bs_name', 'bs_name').distinct
> (),
>         required=False,
>         label='Business Service',
>         widget=SelectMultiple(attrs={'class': 'form-control'}),
>     )
>
>     ...
>
> This worked great until someone else on my team attempted to start with a 
> fresh DB and was getting "relation does not exist" errors trying to do the 
> initial import.
>
> Commenting out these "choices=" parameters in `forms.py` fixes the issue.  
> What's the right way to go about populating form field choices from the DB 
> at access time?
>
> Thanks,
>
> j
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d9a66087-72e5-4540-b717-bbb375f116da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to