[Django] #32325: cannot assing 1 models.id must be "model name" instance

8 views
Skip to first unread message

Django

unread,
Jan 5, 2021, 6:17:54 PM1/5/21
to django-...@googlegroups.com
#32325: cannot assing 1 models.id must be "model name" instance
-----------------------------------------+------------------------
Reporter: slobadsky | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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)

--
Ticket URL: <https://code.djangoproject.com/ticket/32325>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 5, 2021, 6:21:27 PM1/5/21
to django-...@googlegroups.com
#32325: cannot assing 1 models.id must be "model name" instance
-------------------------------+--------------------------------------
Reporter: slobadsky | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

Please see TicketClosingReasons/UseSupportChannels for ways to get help.

--
Ticket URL: <https://code.djangoproject.com/ticket/32325#comment:1>

Reply all
Reply to author
Forward
0 new messages