Making a dropdown list based on an IntegerField's value in django

Viewed 33

I want to display the Nombre_des_Emplacments IntegerField in the Rayons model as a dropdown menu in the References Model below, for example the number inputted is 44, I want for the dropdown menu to show the numbers between 1 and 44 as choices to select from, and after a number is selected and saved after adding the References, it has to be taken out from the choices and not show up again when adding a new Reference, Exp: if 20 is selected next time the dropdown menu will show all the numbers from 1 to 44 except 20

Just so you know the Nombre_des_Emplacments IntegerField is also suppossed to be dependent on Rayons.Nom from the same Model which will also be a dropdown menu

So when adding a reference and I get to the Emplacment field, I select the Rayon and depending on what I selected the second dropdown menu will show the correlated numbers with that Rayons

Here's the code:

models:py

class Rayons(models.Model):
    Nom = models.CharField(max_length=100, blank=False, null=True)
    Nombre_des_Emplacments = models.IntegerField()

    def __str__(self):
        return self.Nom


class References(models.Model):
    Ref = models.CharField(max_length=255, blank=False, null=True)
    Designation = models.CharField(max_length=100, blank=False, null=True)
    Indice = models.CharField(max_length=100, blank=False, null=True)
    Emplacment = models.ForeignKey(Rayons, blank=False, null=True, on_delete=models.CASCADE)
    Client = models.ForeignKey(Client, blank=False, null=True, on_delete=models.CASCADE)
    Statut = models.BooleanField(default=True)

    def __str__(self):
        return self.Ref

forms.py

class RayonsCreateform(forms.ModelForm):
    class Meta:
        model = Rayons
        fields = ['Nom', 'Nombre_des_Emplacments']


    def clean_Nom(self):

        Nom = self.cleaned_data.get('Nom')
        if not Nom:
            raise forms.ValidationError('Ce champs est obligatoire')
        for instance in Rayons.objects.all():
            if instance.Nom == Nom:
                raise forms.ValidationError(str(Nom) + ' existe déja dans la liste ')
        return Nom

views.py

def Ajouter_Rayons(request):
    form = RayonsCreateform(request.POST or None)
    if form.is_valid():
        form.save()
        messages.success(request, 'Ajouté avec succès')
        return redirect('/list_rayons')
    context = {
        "form": form,
        "title": "Ajouter des Rayons",
    }
    return render(request, "Ajouter_Rayons.html", context)

def list_rayons(request):
    header = 'Liste des rayons'
    queryset = Rayons.objects.all()
    form = RayonsSearchForm(request.POST or None)
    context = {
        "header": header,
        "queryset": queryset,
        "form": form,
    }
    if request.method == 'POST':
        queryset = Rayons.objects.filter(Nom__icontains=form['Nom'].value(),
    )
    context = {
        "form": form,
        "header": header,
        "queryset": queryset,
    }
    return render(request, "list_rayons.html", context)

HTML code of the adding of refrences page list_references.html

<main role="main" class="container">
  <div class="jumbotron">
    <h1>{{ title }}</h1>
  <form method='POST' action=''>{% csrf_token %}
        {{form|crispy}}
        <input type="submit" value='Save'/>
    </form>
  </div>

</main>
0 Answers
Related