Sorry I used the code icon from the toolbar.
I will take the advice about lowercase. I come from another language
Sorry I used the 3 quotes for the code but it doesn’t work for me
I do it like this
‘’’
code…
‘’’
Should I do it any other way ?
I will specify the question
I have a models
Utente
‘’’
class Comuni(models.Model):
CodCatastale = models.CharField(max_length=4, unique=True, default=‘’, blank=False, null=False)
Comune = models.CharField(max_length=30, unique=True, default=‘’, blank=False, null=False)
Cap = models.IntegerField(validators=[MaxValueValidator(99999), MinValueValidator(0)], blank=True, null=True)
Prov = models.CharField(max_length=2, default=‘’, blank=True, null=True)
RegioneID = models.ForeignKey(Regioni, on_delete=models.PROTECT)
class Meta:
ordering = ['Comune']
def __str__(self):
return self.Comune
‘’’
Another Models
‘’’
Utente
class Utente(models.Model):
RagSociale = models.CharField(max_length=40, blank=False, null=False)
Indirizzo = models.CharField(max_length=30, default=‘’)
ComuneID = models.ForeignKey(Comuni, on_delete=models.PROTECT, null=True)
Cap = models.IntegerField(validators=[MaxValueValidator(99999), MinValueValidator(0)], blank=True, null=True)
#Prov = models.CharField(max_length=2, default=‘’, blank=True, null=True)
Telefono = models.IntegerField(null=True, blank=True)
Cellulare = models.IntegerField(null=True, blank=True)
eMail = models.EmailField(max_length=50, default=‘’, blank=True, null=False)
PEC = models.EmailField(max_length=50, default=‘’, blank=True, null=False)
SitoInter = models.CharField(max_length=25, default=‘’, blank=True, null=True)
CodFiscale = models.CharField(max_length=16, default=‘’, blank=True, null=True)
PartitaIva = models.IntegerField(null=True, blank=True)
Segretario = models.CharField(max_length=25, default=‘’, blank=True, null=True)
Presidente = models.CharField(max_length=25, default=‘’, blank=True, null=True)
@property
def Prov(self):
return self.ComuneID.Prov if self.ComuneID else None
def __str__(self):
return self.RagSociale
‘’’
As you can see the ComuniID field is linked with the ID of the Comuni Table.
In editing the User record, from the select I choose the Municipality and it should fill me the Cap field of the form with the value of the Cap field present in the Municipalities Table
I wrote a js function and get the municipality ID, but then I don’t know how to filter the movement in the Municipalities Table
JS
‘’’
document.addEventListener(“DOMContentLoaded”, function () {
const comuneInput = document.querySelector(“#id_ComuneID”).value;
const capInput = document.querySelector(“#id_Cap”).value;
alert(`GAE COMUNE è ${comuneInput}`);
alert(`GAE CAP è ${capInput}`);
comuneInput.addEventListener("blur", function () {
alert(`MODIFICA`);
const selectedComuneCap = getCapValueForComune(this.value);
capInputUtente.value = selectedComuneCap;
});
});
‘’’