Hey Feroz,
Since you mentioned getting the data by "previous month", let's start
there. Your month field is a CharField so I assume your storing the
names of the months in there. If so, you'll need a way to figure out
what the previous month is. Let's say the current month is February,
you could use a list of month names[1] and lookup the index of the
current month:
month_names = list(calendar.month_name)[1:]
month_index = month_names.index('February')
Then get the previous month by subtraction:
previous_month_index = (month_index - 1) % 12
previous_month = month_names[previous_month_index]
But you have another problem. You're not keeping track of the year.
So, at most you can only store one year's worth of salary records.
So at this point, I'd recommend replacing the CharField month field with
a DateField. That solves your problem of keeping track of the year.
Then you can also use some date math[2] to calculate the previous month.
Once you have the previous month, you can use that to: lookup that model
instance, copy it[3], update the month to the current month, then save.
As an aside, I'd rename your model class to something that makes more
sense, from 'Employee' to 'PayRecord`
Hope this helps!
Ryan N
[1]
https://docs.python.org/3/library/calendar.html#calendar.month_name
[2]
https://stackoverflow.com/a/9725093/226697
[3]
https://docs.djangoproject.com/en/3.2/topics/db/queries/#copying-model-instances