You need to keep track of the month and if it changes increase the month_id as below
def create_date_table():
d1 = date(2021, 1, 1)
d2 = date(2022, 12, 31)
delta = d2 - d1
dates = []
date_id = 1
month_id = 1
last_month = d1.month
for i in range(delta.days + 1):
new_date = (d1 + timedelta(days=i))
full_date = new_date.strftime('%Y-%m-%d')
dates.append({'id': date_id,
'month_id': month_id,
'date': full_date})
date_id+=1
#increase the month_id here when month changes
if last_month != new_date.month:
last_month = new_date.month
month_id+=1
print(dates)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…