[Django]-Django CreateView – custom get_queryset for only one of the fields

3👍

It’s not get_queryset you want to override here. Rather, you need to create a form, and define a custom queryset on the Vessel field:

class MissionForm(forms.ModelForm):
    vessel = forms.ModelChoiceField(queryset=Vessel.objects.all().order_by('name'))
    class Meta:
        model = Mission
        fields = ['title', 'vessel', 'status']

class MissionCreate(CreateView):
    form_class = MissionForm
    model = Mission

Leave a comment