How to delete user salary record , but want to keep history till deleted date

73 views
Skip to first unread message

Feroz Ahmed

unread,
Jan 7, 2022, 8:53:14 AM1/7/22
to Django users
Hi,
How to delete user salary record , but want to keep history till deleted date.

ex:
  1. Month June
  2. Month July
  3. Month Aug
  4. delete user record from Sep.

pankaj palmate

unread,
Jan 7, 2022, 8:59:18 AM1/7/22
to django...@googlegroups.com
Then simply make that records inactive by adding one flag in table and instead of deleting make It inactive 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f2ba2f5f-ed12-45e5-8d2a-d2b1b2590154n%40googlegroups.com.

Feroz Ahmed

unread,
Jan 7, 2022, 10:48:20 AM1/7/22
to Django users
Hi, Pankaj
thanks for the tips!

i try all the way, 
it not success , any syntax please.
mean time i success to delete the record code as below: (but i just want to inactive the record of particular employee) history might need for audit purpose.

def delete(request,id):
        employee=Employee.objects.get(id=id)
        employee.delete()
        return redirect('/home1')

Regards

Julio Cojom

unread,
Jan 7, 2022, 12:13:54 PM1/7/22
to django...@googlegroups.com
As Pankaj mentioned, add a field in your model called status or something like that. On the makemigrations set all status as 'Active'

Then, your function will not delete, will update the object.

def delete(request,id):
        employee=Employee.objects.get(id=id)
        employee.status = 'Inactive'
        return redirect('/home1')

in your listview, you should only show active records, for that add an exclude to the queryset.

queryset = Model.objects.exclude(status='Inactive') 

or with a filter

queryset = Model.objects.filter(status='Active') 


If you want to use delete, I've used before django-simple-history. the package records all history for models and objects and let you query that information.


Regards,

Julio Cojom.




Feroz Ahmed

unread,
Jan 8, 2022, 9:17:17 AM1/8/22
to Django users
No errors: but no any actions, 
all records shows in home1.html
not excluding the Inactive records.


def delete(request,id):
        employee=Employee.objects.get(id=id)
        employee.status='Inactive'
        return redirect('/home1')

--------------------------------------------
#listview

def home1(request):
        employees=Employee.objects.exclude(status='Inactive')
        return render(request,'asgapp/home1.html',{'e':employees})

Feroz Ahmed

unread,
Jan 8, 2022, 9:30:26 AM1/8/22
to Django users
Hi , it works,
it was my confusion,, 
i update record status as Inactive so , it hide the record.

thanks for your support

Reply all
Reply to author
Forward
0 new messages