Home > Net >  How to compare a two table values in django using filter
How to compare a two table values in django using filter

Time:12-27

I need to compare the row values of Specific_product table (product) and Product table (name) using the sp.product

def count_time(request):

    sp = Specific_product.objects.filter(boolean=False)
    products = Product.objects.all().filter(name={{sp.product}})
    

    stu={
        "pro_d":sp,'products': products,'purchases':purchases
    }

    return render(request, 'mf_ta/product_status.html', stu)

How can I satisfy this condition?

I'm getting this error

AttributeError at /count_time/
'QuerySet' object has no attribute 'product'
Request Method: GET
Request URL:    http://127.0.0.1:8000/count_time/
Django Version: 4.0.7
Exception Type: AttributeError
Exception Value:    
'QuerySet' object has no attribute 'product'
Exception Location: C:\Users\Admin\PycharmProjects\pythonProject1\invention_on_macroalgae\manufacturing_team_app\views.py, line 56, in count_time
Python Executable:  C:\Users\Admin\PycharmProjects\pythonProject1\venv\Scripts\python.exe
Python Version: 3.9.0
Python Path:    
['C:\\Users\\Admin\\PycharmProjects\\pythonProject1\\invention_on_macroalgae',
 'C:\\Users\\Admin\\PycharmProjects\\pythonProject1',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\Admin\\PycharmProjects\\pythonProject1\\venv',
 'C:\\Users\\Admin\\PycharmProjects\\pythonProject1\\venv\\lib\\site-packages']
Server time:    Mon, 26 Dec 2022 03:54:39  0000

CodePudding user response:

.filter returns a QuerySet not a single object. You probably need one of:

sp = Specific_product.objects.filter(boolean=False).first()
sp = Specific_product.objects.filter(boolean=False).last()
sp = Specific_product.objects.get(boolean=False)

CodePudding user response:

try this

def count_time(request):

    sp = Specific_product.objects.filter(boolean=False)
    products = Product.objects.all().filter(name__in=list(sp.values('product', flat=True)))
    products = Product.objects.all().filter(name__in=list(sp.values('product__name', flat=True)))

    stu = {
        "pro_d": sp, 'products': products, 'purchases': purchases
    }

    return render(request, 'mf_ta/product_status.html', stu)
  • Related