我在我的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\\\'"\'', '\'"\\\'\\\'\'']
我已经给出了上面输出中使用的代码,你能帮我吗?
1条答案
按热度按时间q8l4jmvw1#
尝试仅添加不带元组的数据,以便您可以连接和保存数据