from django import forms from .models import Content, Category class CreateContent(forms.ModelForm): class Meta: model = Content fields = [ 'name', 'category', 'file', ] def __init__(self, school, *args, **kwargs): super().__init__(*args, **kwargs) self.instance.school = school already_created = map(lambda x:x.category.pk, school.content_set.select_related('category')) self.fields['category'].queryset = Category.objects.exclude(pk__in=already_created)