Let’s say I have a variable time_period, with a value of one of the following strings: weekly, monthly, yearly.
…and a QuerySet call filtering through a model TimeModel:
time_period = 'monthly'
instance = TimeModel.objects.get(time_period=time_period,
year=datetime.now().year,
month=datetime.now().month)
In this case, I’ve passed the parameters year and month in, because time_period == 'monthly'. If time_period == yearly, I would want to only pass year, and if time_period == weekly, I would pass all three (yearly, monthly, and weekly) in.
Is there any way of doing what I’ve described short of writing 3 if statements?
Maybe this helps: