Salary Generate

37 views
Skip to first unread message

Feroz Ahmed

unread,
Feb 15, 2022, 9:32:34 AM2/15/22
to Django users

Dear All.
i am stuck at point to generate salaries for upcoming month,
as i already have last month January data as fields(month, name , gross sal, tax deduction, net sal)
and it has records of 35 (employees)
in form template i have placed submit button to generate salaries for February.

****Plz help how to get all get data as it is from previous Month  month 
AND new records to be as for month February

****** month field data for 35 record to be as new records and month values as **February
data file attached.

Class employee(models. Model)
month=models.CharField(max_length=64)
  name= models.CharField(max_length=64)
  gross sal= models.IntegerField()
  tax= models.IntegerField()
net sal= models.IntegerField()




salary.jpg

Ryan Nowakowski

unread,
Feb 25, 2022, 3:55:58 PM2/25/22
to Django users
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

Reply all
Reply to author
Forward
0 new messages