Opened 4 years ago
Closed 4 years ago
#32325 closed Uncategorized (invalid)
cannot assing 1 models.id must be "model name" instance
Reported by: | slobadsky | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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())
contextlocataire_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)
Please see TicketClosingReasons/UseSupportChannels for ways to get help.