﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32325	"cannot assing 1 models.id must be ""model name"" instance"	slobadsky	nobody	"hi i am very new to Django and trying to insert the datas into a table and i get ""Cannot assign ""1"": ""Indexation.loc_id"" must be a ""Locataire"" instance."" i do not know what and where i made a mistake?

views.py

def nouvelle_indexation(request):
    context={}
    i=Indexation()
    ts = str(time.time())
    context['locataire_names']=Locataire.objects.all().values('loc_id','loc_name').filter(loc_active=1).order_by('loc_name')
    if request.POST.get('date_prelev') and request.POST.get('foreign_loc_id') and request.POST.get('o_index') and request.POST.get('n_index'):            
        i.date_index_prelev= request.POST.get('date_prelev')
        i.old_index= request.POST.get('o_index')
        i.new_index =request.POST.get('n_index')
        i.indexation_unique_ref=str(uuid.uuid5(uuid.NAMESPACE_DNS,ts)).replace(""-"", """")
        i.loc_id=int(request.POST.get('foreign_loc_id'))
        print(i.loc_id)
        #i.save()
        context={            
            'message':'L\'indexation a bien été ajouté.',
        }
    return render(request,'nouvelle-indexation.html',context)

models.py

class Locataire(models.Model):
    loc_id=models.AutoField(primary_key = True)
    loc_name= models.CharField(max_length=100)
    loc_loyer= models.DecimalField(max_digits=5, decimal_places=2, null=True)
    loc_charge= models.DecimalField(max_digits=5, decimal_places=2, null=True)
    loc_active= models.BooleanField(default=1)
    loc_unique_ref=models.CharField(max_length=100,editable=False)
    loc_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return ""%s"" % (self.loc_name)

class Charge(models.Model):
    year_id=models.AutoField(primary_key = True)
    charge_year= models.IntegerField(unique=True)
    ord_men= models.DecimalField(max_digits=4, decimal_places=2)
    eau_mcube= models.DecimalField(max_digits=4, decimal_places=2)
    charge_unique_ref=models.CharField(max_length=100,editable=False, null=True)
    def __str__(self):
        return ""%s"" % (self.charge_year)


class Indexation(models.Model):
    index_id=models.AutoField(primary_key = True)
    date_index_prelev= models.DateField()
    old_index= models.IntegerField()
    new_index=models.IntegerField()
    indexation_unique_ref=models.CharField(max_length=100,editable=False,null=True)
    loc_id=models.ForeignKey(Locataire, blank=True, null=True, on_delete=models.SET_NULL, default=None)
    charge_id=models.ForeignKey(Charge, blank=True, null=True, on_delete=models.SET_NULL, default=None)
    def __str__(self):
        return ""%s"" % (self.date_index_prelev)"	Uncategorized	closed	Uncategorized	3.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
