Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Trying building your queryset using the pythons union operator -</p> <pre><code>class PrimaryCategoryForm(forms.ModelForm): set_1 = PrimaryAdCategoryType.objects.filter(pk=2) set_2 = PrimaryAdCategoryType.objects.filter(pk=3) qs = set_1 | set_2 primarycategorytype = forms.ModelMultipleChoiceField(queryset=qs, required=True, widget=forms.CheckboxSelectMultiple) </code></pre> <p>The <code>|</code> operator is creating a new set with all items from <code>set_1</code> and all items from <code>set_2</code>. </p> <p>UPDATE</p> <p>Whilst the method above will support creating complicated sets. Martin's suggestion in the comments is probably sufficient if you simply want to pick a few pk's -</p> <pre><code>class PrimaryCategoryForm(forms.ModelForm): qs=PrimaryAdCategoryType.objects.filter(pk__in=[2,3]) primarycategorytype = forms.ModelMultipleChoiceField(queryset=qs, required=True, widget=forms.CheckboxSelectMultiple) </code></pre> <p>EDITABLE CHECKBOXES</p> <p>I think when it comes to making the checkboxes editable and preselected you may need to resort to javascript. Perhaps <a href="https://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs" rel="nofollow">add data-attributes to the widget</a>, then create a jquery function that manipulates the checkboxes on the basis of the data-attributes. Something like -</p> <pre><code>attrs = {'data-selected-value': 2, 'data-readonly-value': 2} primarycateogrytype = forms.ModelMultipleChoiceField(attrs=attrs) </code></pre> <p>Then in the javascript -</p> <pre><code>var readonlyCheckboxValue = $('#your_mulliplecheckboxselect).data('selected-value'); var $readonlyCheckbox = $('#your_multiplecheckboxselect .input[value=readonlyCheckboxValue]'); $readonlyCheckbox.attr('readonly', true); </code></pre> <p>I haven't tested this code, but it should give you an idea of what I'm suggesting. Do check out the <a href="http://www.w3.org/TR/2010/WD-html5-20101019/elements.html#embedding-custom-non-visible-data-with-the-data-attributes" rel="nofollow">data-attribute</a> if you haven't come across it already - it's pretty awesome.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload