I created a list using the getlist
method in my django project, and then I use this list in my form. Every time I update the form, it automatically adds a new character to the list. How can I prevent this? I used the strip and split methods but no results. I am attaching my codes below.
models.py
class Records(models.Model):
medications = models.CharField(max_length=1000, null=True,blank=True)
scale_used = models.CharField(max_length=1000, null=True,blank=True)
way_of_application = models.CharField(max_length=1000,null=True,blank=True)
views.py
def insertList(request):
records = Records()
records.medications = request.POST.getlist('medications')
records.scale_used = request.POST.getlist('scale_used')
records.way_of_application = request.POST.getlist('way_of_application')
records.save()
return redirect('/index/tables')
def getList(request,id):
edit_records = Records.objects.get(id=id)
if edit_records.medications is not None:
edit_records.medications = edit_records.medications.strip('"]["').split(',')
if edit_records.scale_used is not None:
edit_records.scale_used = edit_records.scale_used.strip('"]["').split(',')
if edit_records.way_of_application is not None:
edit_records.way_of_application = edit_records.way_of_application.strip('"]["').split(',')
return render(request,"update_record.html",{"Records": edit_records})
Output:
medications = ['\'\\\'PULCET 40MG FLAKON\\\'"\'', '\'"\\\'METPAMİD 10MG/2ML AMPUL\\\'"\'', '\'"\\\'İZOTONİK 250ML\\\'"\'']
scale_used = ['\'\\\'1 FLAKON\\\'"\'', ' \' " \\\'1 AMP\\\'"\'', ' " \' 1 ADET\'"', ' \' " \\\'\\\'\'']
way_of_application = ['\'\\\'I.V İnfüzyon\\\'"\'', '\'"\\\'I.V\\\'"\'', '\'"\\\'I.V\\\'"\'', '\'"\\\'\\\'\'']
I have given the codes I used with the outputs above, can you help with this?
CodePudding user response:
Try to add only the data without tuple for that you can join and save the data
def insertList(request):
records = Records()
records.medications = ','.join(request.POST.getlist('medications'))
records.scale_used = ','.join(request.POST.getlist('scale_used'))
records.way_of_application = ','.join(request.POST.getlist('way_of_application'))
records.save()
return redirect('/index/tables')