html 删除使用getlist方法创建的列表中的字符

oiopk7p5  于 2022-12-09  发布在  其他
关注(0)|答案(1)|浏览(101)

我在我的django项目中使用getlist方法创建了一个列表,然后我在我的表单中使用了这个列表。每次我更新表单时,它都会自动向列表中添加一个新字符。我如何防止这种情况?我使用了strip和split方法,但没有结果。我在下面附上了我的代码。
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})

输出:

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\\\'"\'', '\'"\\\'\\\'\'']

我已经给出了上面输出中使用的代码,你能帮我吗?

q8l4jmvw

q8l4jmvw1#

尝试仅添加不带元组的数据,以便您可以连接和保存数据

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')

相关问题