Django get\u或\u create将数据保存在括号中

2024-10-02 08:24:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用django的get_or_create将数据保存到postgres。代码工作正常,但是itemgrp1hd字段保存为('Mobile 5010',),而我只输入了Mobile 5010。有人能解释一下为什么括号和单引号在保存在postgres中时会出现吗。你知道吗

代码如下:

@api_view(['GET', 'POST', 'PUT', 'DELETE'])
def Post_Items_Axios(request):

    data_itemfullhd = request.data['Item Name']
    data_itemgrp1hd = request.data['Item Group1']

    td_items, created = Md_Items.objects.get_or_create(
        cunqid = entity_unqid,
        itemfullhd = data_itemfullhd,
        # defaults = dict(
        #   itemgrp1hd = data_itemgrp1hd,
        #   )
        )

    # type(request.data['Item Group1']) 
    # <class 'str'>
    td_items.itemgrp1hd = data_itemgrp1hd,

    td_items.save()

    data = {'data_itemfullhd': data_itemfullhd}
    return Response(data)

Tags: or代码datagetrequestcreateitemspostgres
1条回答
网友
1楼 · 发布于 2024-10-02 08:24:32

你必须去掉第15行末尾的逗号。你知道吗

更改''''td_items.itemgrp1hd项目=数据项GRP1HD, td公司_项目.保存()“”

''td_items.itemgrp1hd项目=数据项GRP1HD td公司_项目.保存()“”

结尾有一个逗号告诉Python您希望它保存在一个元组中。你知道吗

有关尾随逗号和元组的更多信息,请参见此问题。你知道吗

What is the syntax rule for having trailing commas in tuple definitions?

相关问题 更多 >

    热门问题