Django:我应该如何更正此表单以在下拉列表中显示模型的数据

Django : How should I correct this Form to display the data of the models in a dropdown list

我是 Django 的 Newbee。我想为下拉列表创建一个表单。下拉列表应根据用户和模型显示不同的选择。所以要得到我想在下拉列表中显示的数据。我开始获取用户 ID。然后从用户 ID 中,我获得了该用户在模型“TblAadJntGroup”中可以访问的 ID 客户端列表。最后,我通过在模型“TblAadGroups”中获取此 ID 列表的相关性来识别客户端列表。

我根据下面收听的模型编写此表格。它得到了“id_client_list”,但我不知道如何在init方法之后的“forms.ChoiceField”中添加它。我应该改变或改进什么

models.py

class TblAadJntGroup(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
    id_aadgroup = models.IntegerField(db_column='ID_AADGroup', blank=True, null=True)  # Field name made lowercase.
    id_aaduser = models.IntegerField(db_column='ID_AADUser', blank=True, null=True)  # Field name made lowercase.
    createdon = models.DateTimeField(db_column='CreatedOn', blank=True, null=True)  # Field name made lowercase.
    createdby = models.CharField(db_column='CreatedBy', max_length=255, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'tbl_AAD_jnt_GroupMember'

class TblAadGroups(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
    idcustomer = models.IntegerField(db_column='IDCustomer', blank=True, null=True)  # Field name made lowercase.
    groupname = models.CharField(db_column='GroupName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    samaccountname = models.CharField(db_column='SamAccountName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    groupcategory = models.CharField(db_column='GroupCategory', max_length=255, blank=True, null=True)  # Field name made lowercase.
    groupscope = models.CharField(db_column='GroupScope', max_length=255, blank=True, null=True)  # Field name made lowercase.
    displayname = models.CharField(db_column='DisplayName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    groupdescription = models.CharField(db_column='GroupDescription', max_length=255, blank=True, null=True)  # Field name made lowercase.
    timestampm = models.DateTimeField(db_column='TimeStampm', blank=True, null=True)  # Field name made lowercase.
    username = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tbl_AAD_Groups'

html

{% extends 'home.html' %}

{% load static %}

{% block content %}
<link rel="stylesheet" href="{% static 'test12.css' %}">
<div class="upload-app">
    <div class="selectclients">
        <h2>Select The Company</h2>
        <form method="post" id="SelectCompany">
            {% csrf_token %} 
            {{ form_client.as_p }}
            <input type="submit" value="Select">
        </form>
    </div> 
</div> 
{% endblock %}

编辑:forms.py

class SelectClient(forms.ModelForm):

    
    def __init__(self, *args, **kwargs) :
        self.user = kwargs.pop('user')
        super(SelectClient, self).__init__(*args, **kwargs)

        id_client_list = TblAadJntGroup.objects.filter(id_aaduser=self.user.id).values_list('id_aadgroup', flat=True)
        id_client_list = list(id_client_list)

        self.fields['ID_Customer'].queryset = TblAadGroups.objects.all().filter(id__in=id_client_list)
        
        ID_Customer = forms.ModelChoiceField(queryset=None)

我收到一个我没有收到的错误:

 __init__
    raise ValueError('ModelForm has no model class specified.')
ValueError: ModelForm has no model class specified.

要更正您的错误,您应该为您的 ModelForm 定义一个模型。 示例:

class SelectClient(forms.ModelForm):

    class Meta:
        model = TblAadGroups
        fields = '__all__'

    def __init__(self, *args, **kwargs) :
        ...
        ...