[Answered ]-Django objects.count() in default method parameter returns 0

2👍

Function argument evaluates once at the definition time so you shouldn’t use any calculations in the def statement. Workaround for this situation is:

def createTestProduct(product_group="VG", opno=None):
    if opno is None:
        opno = "MyProduct%s" % Product.objects.count()
    ...

Leave a comment