I.Jokhadze I.Jokhadze - 10 months ago 73
Python Question

Django make variable visible

I want to make variable visible in elif without making it global, is it possible in any other ways?

if query !=something:
all_toys = Toys.objects.filter(user_name=request.user)
elif query2 != something:
all_toys = all_toys.filter(amount=20)

second all_toys isn't visible, and i got this error django referenced before assignment.

Answer Source

Another way to this is to create filter dictionary

filter_kw = {}
if query !=something:
    filter_kw['user_name'] = request.user
elif query2 != something:
    filter_kw['amount'] = 20
all_toys = Toys.objects.filter(**filter_kw)