[Fixed]-Create a queryset to compare two models

1👍

Ok well given your current models, here is a possible solution to the problem you are having:

for backlog in Backlog.objects.all():
    try:
        material = Material.objects.get(product_id = backlog.product_id)
        if material.tan_id[0:2] == '74':
            # Classify as 1
        elif material.tan_id[0:2] == '80':
            # Classify as 3
        else:
            # Classify as 2
    except Material.DoesNotExist:
        print("This material is not in backlog")
        continue

This code should loop over every instance of Backlog you have in your database and then try to find the associated Material. In the event it doesn’t find a Material (in your case there is no backlog), objects.get() raises an exception that it doesn’t exist, we print it’s not there and continue on with the loop. If it is we classify it as you specified. Might require a slight bit of tweaking but it should give you the bones of what you want to fix this problem. Let me know if it doesn’t.

Leave a comment