models.py
from django.db import models
class Tag(models.Model):
name = models.CharField('タグ名', max_length=255)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=255)
tag = models.ManyToManyField(Tag)
def __str__(self):
return self.title
forms.py
from django import forms
from .models import Post, Tag
class MyModelChoiceField(forms.ModelChoiceField):
def to_python(self, value):
if value in self.empty_values:
return None
return Tab.objects.filter(name__in=value)
class TestForm(forms.Form):
check = forms.MyModelChoiceField(queryset=Tag.objects.all().values_list('name', flat=True), widget=forms.CheckboxSelectMultiple, empty_label=None)