Fill a field via a select with PrimaryKey

Hello in a form I have several fields in the User models. One of them is a PrimaryKey with name ComuneID.
The primaryKey is linked to another Models called Commons.
In the view view I inserted this code

user = User.objects.first()
prov = user.Prov

to get the value of the Prov field from the Commons table and it works.
Now I would like to do the same thing in edit
As I understand it I have to use js to do this.
In the js file I can get the ID of the chosen municipality but I didn’t understand how to connect to the Commons models pee get the value of the Prov field and display it
I hope I have been clear
Thanks

I’m not sure I understand what you’re trying to do here. Can you post the code for your models?

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. This forces the forum software to keep your code properly formatted.

Also, I strongly encourage you to adopt the Python / Django naming conventions for classes and variables. Only your class names should be capitalized. All function and variable names should be lower case.

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;
});

});
‘’’