ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.

在使用pythondjango程序的时候会报以下错误

raise ValueError

ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.

英文的大概意思是,查找的条件是一个结果集,需要做一个限制结果集

源代码如下:

        Customer_id = data_c.get('Customer_id', '')
        print(Customer_id)
        mc = MCertificate.objects.filter(Customer=Customer_id)
        print(mc)
        mcd = MCertificateDetails.objects.filter(Certificate=mc)
        print(mcd)

修改为:

        Customer_id = data_c.get('Customer_id', '')
        print(Customer_id)
        mc = MCertificate.objects.filter(Customer=Customer_id)
        print(mc)
        # 修改下面的代码需要加一个__in 即可解决这个问题
        mcd = MCertificateDetails.objects.filter(Certificate__in=mc)
        print(mcd)