0

I have the following django app structure:

models.py

class CodiceCommessa(models.Model):
    codice_commessa=models.ForeignKey(Informazioni_Generali, on_delete=models.CASCADE, null=True)

class Informazioni_Generali(models.Model):
    codice_commessa= models.CharField('Codice', unique=True, max_length=5)
    data_registrazione=models.DateField()

forms.py

class CodiceCommessaForm(forms.ModelForm):
    class Meta:
        model = CodiceCommessa
        fields = "__all__"

views.py

def altri_costi_commessa(request):
    cod=[]

    if request.method == 'POST':
        year = request.POST['year']

    if request.method == 'POST':
         form = CodiceCommessaForm(request.POST)
         if form.is_valid():
             cod = form.save()
    else :
        form = CodiceCommessaForm()

In my template I have set the form. All works, but I want that, when I select a year, automatically django show me only the codice_commessa that match the year in the field data_registrazione.

Is it possibile to get it?

Federico De Marco
  • 341
  • 1
  • 5
  • 20
  • Does this answer your question? [Django dependent select](https://stackoverflow.com/questions/25706639/django-dependent-select) – JPG Jul 15 '20 at 10:47
  • On top of what @ArakkalAbu is linking (which are older custom solutions), [Django Autocomplete Light](https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#filtering-results-based-on-the-value-of-other-fields-in-the-form) is the de-facto tool for this. –  Jul 15 '20 at 10:51

1 Answers1

0

i didn't get your question properly but for searching anything from database you can use following syntex x = labor.objects.filter('COMPRESSION STATEMENTS').value('KEY FOR SEARCHING') x = labor.objects.filter(PhoneNumber=MobNo).values('AdharNo')

any only for searching object use x = labor.objects.filter(PhoneNumber=MobNo)

Milan
  • 46
  • 7