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

Time:03-22

I am Newbee in Django. I want to create a Form for a dropdown list. The Dropdown list should show different choices depending on User and the Models. So to get the data that I want to display in the dropdown list. I start to get the user ID. Then from the User ID, I get the list of ID clients that this user has access in the model "TblAadJntGroup". Finally I identify the client list by getting the correlation of this list of ID in the model "TblAadGroups".

I write this form from the models that are listen below. It gets well the "id_client_list", but I do not know how to add it in the "forms.ChoiceField" after the init method. What should I change or improve

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 >
    <div >
        <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 %}

Edit : 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)

I get an error that I do not get :

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

CodePudding user response:

To correct your error, you should define a model for your ModelForm. Example:

class SelectClient(forms.ModelForm):

    class Meta:
        model = TblAadGroups
        fields = '__all__'

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