[Django]-Django: How to limit_choices_to when using custom intermediate table

2👍

You could set the choices in the __init__ method of the form:

class PlanetForm(ModelForm):

    class Meta:
        model = Planet
        fields = ['name', 'samples']

    def __init__(self, *args, **kwargs):
         super(PlanetForm, self).__init__(*args, **kwargs)

         sample_choices = list(
             Sampletype.objects.filter(sample_option=True).values_list('id', 'name')
         )
         # set these choices on the 'samples' field.
         self.fields['samples'].choices = sample_choices
👤AKS

Leave a comment