如何根据Django数据库中实例之间的关系删除选择字段

How to remove the choice field based on relationships between instances in the database in Django

我是 django 的新手,我想问一个关于我无法解决的案例的问题,例如我有很多类别,在这些类别中我可以有一个或多个固定部分,例如。 section = [a, b, c, d, e] 所以如果我有一个显示这些选项的模块,但我已经在类别“1”中有“b”部分,我如何从列表中删除选项“b”以便拥有最多一个类别由 5 个非重复部分组成

class 部分表格(forms.ModelForm):

class Meta:
    model = Section
    fields = ["nome"]


def __init__(self, *args, **kwargs,):
    section_pk = kwargs.pop('section_pk', None)
    super(CategotyForm, self).__init__(*args, **kwargs)
    key = Section.objects.filter(category=section_pk).values_list()
    list_key = list(key)
    exist_section = []
    total_section = ['Section A','Section B','Section C','Section D','Section E']
    for i in range(len(list_key)):
        exist_section.append(list_key[i][1])
    choice = list(set(total_section)- set(exist_section))
    for i in range(len(choice)):
        choice[i] = choice[i].title()
    dict_choice = []
    A = 'Section A'
    B = 'Section B'
    C = 'Section C'
    D = 'Section D'
    E = 'Section E'
    label = ''
    for i in range(len(choice)):
        if choice[i] == 'Section A':
            label = A
        if choice[i] == 'Section B':
            label = B
        if choice[i] == 'Section C':
            label = C
        if choice[i] == 'Section D':
            label = D
        if choice[i] == 'Section E':
            label = E
        dict_choice.append(label)
    new_choice = list(zip(dict_choice, choice))
    self.fields["name"] = forms.ChoiceField(choices=new_choice)