将列表作为多行保存到数据库

时间:2017-12-05 17:59:38

标签: django

我有一个列表,该列表来自如下定义的复选框选择:

checkedlist = request.GET.getlist('report_id')

此列表会被发布到提交的视图。

def submitted(request):

    owner = User.objects.get (formattedusername=request.user.formattedusername)
    requestsave = QVFormAccessRequest(ntname_id = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid, facility = owner.facility, title = owner.title
                                      ,report_id = request.POST.getlist('report_id'))

    requestsave.save()

问题在于,当它尝试保存到数据库时,我的字段被定义为正确的int。我希望列表的report_id存储在我的数据库中的多行,如下所示:

user information report_id
user1234         1
user1234         2
user1234         3
user1234         4

在当前保存中,它试图将列表发布到report_id,如何将其分成多行,如上所示?我收到以下错误:

TypeError at /account/submitted/
int() argument must be a string, a bytes-like object or a number, not 'list'

1 个答案:

答案 0 :(得分:1)

怎么样

my_list = request.POST.getlist('report_id')
for i in my_list
    requestsave = QVFormAccessRequest(ntname_id = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid, facility = owner.facility, title = owner.title, report_id = i)
    requestsave.save()