0

I am populating my drop down box with values from my model, I have five values in my model, I only want to show three of these values in this particular instance how would I achieve this.

forms.py

class namesForm(forms.Form):
    names = forms.ModelChoiceField(
        queryset=Names.objects.order_by('name').exclude(name='Josh','Tom'),
        label = "Name:",
        widget = Select(attrs={'class': 'span6 small-margin-top small-margin-bottom'}),
        empty_label = "Select a Name",
        required=True
    )
1
  • You are using very specific rules in your exclusion, is that the real case for your question? Commented Feb 14, 2017 at 15:43

1 Answer 1

1

Based on your forms.py code, i assume this is what you want:

class namesForm(forms.Form):
    names = forms.ModelChoiceField(
        queryset=Names.objects.exclude(name__in=['Josh','Tom']).order_by('name'),
        label = "Name:",
        widget = Select(attrs={'class': 'span6 small-margin-top small-margin-bottom'}),
        empty_label = "Select a Name",
        required=True
    )

Line I changed:

queryset=Names.objects.exclude(name__in=['Josh','Tom']).order_by('name'),

Documentation on using __in:
https://docs.djangoproject.com/en/1.10/ref/models/querysets/#in

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.