我想在前端显示重复值错误

时间:2018-01-08 05:53:58

标签: django python-3.x

我在数据库中有一个query_name字段。我希望每个值都应该是唯一的,所以我改变了它的约束并添加了unique = true。 现在我希望如果用户输入重复值,则向用户显示错误重复值。目前错误仅在后端显示

这是我在python中的代码

def save_report(request):
    if request.method == 'POST':
        print(request.POST.dict())

        querydata = ReportCreationData.objects.all()
        querydata_list = []
        querydata_dict = {'query_name':''}
        for data in querydata:
            querydata_dict['query_name'] = data.query_name
            print ('querydata_dict', querydata_dict)



        data_dict  = request.POST.dict()
        query_name = data_dict['query_name'],
        print ('query_name', query_name)

        query_json = {}
        #query json data
        query_json['data_src_name'] = data_dict['data_src_name']
        query_json['fields']        = data_dict['fields']
        query_json['group_by']      = data_dict['group_by']
        query_json['order_by']      = data_dict['order_by']
        query_json['where']         = data_dict['where']
        query_json['limit']         = data_dict['limit']
        query_json                  = json.dumps(query_json)


        report_creation_obj = ReportCreationData.objects.create(
                            query_json       = query_json, 
                            data_source_name = data_dict['data_src_name'], 
                            query_name       = data_dict['query_name'], 
                            mail_body        = data_dict['mail_body'])



        if (query_name == querydata_dict).exists():

            raise ('already exists')

        else:
            report_creation_obj.save()

        return HttpResponse('success')
    else:
       return render(request, 'home/report_creation.html')

with code 2 getting error:
AttributeError: 'bool' object has no attribute 'exists'

代码2:

{{1}}

请帮忙

感谢

1 个答案:

答案 0 :(得分:1)

您可以在插入数据之前尝试,从数据库运行select查询并在 query_name 上应用where子句和当前值。因此,您可以获得重复记录。