Hi there once again,
although I have solved more problems in my project than I have left (also because of the helpful brains of all of you in here ) there are some left:
Let’s say we have a database of employees, and a On-To-Many relationship to their employments.
Adittionally we have some laws concerning data-security, that make it neccessary to store a Boolean keep_data
field, depending on the employee having decided to want his data deleted after Employment or not.
Additionally, after an Employee has retired, only people with the right user-role still can see their data.
And finally: an Employee can have more than one employment over the time. Not simultaneously of course but one after another on a timeline.
So if i try to handle this in displaying datasets by appropriate filtering, i have the folowing for the use case, that i want get displayed a “retired employee” Message in the database view of this special employee:
if Employments.objects.filter(employee_id=employee).exists():
context['no_employment'] = 'False'
current_employment = Employments.objects.filter(employee_id=employee).latest('duty_begin')
filters &= Q(duty_end__lt=datetime.date.today()) & Q(duty_begin=current_employment.duty_begin)
filters &= Q(keep_data=1)
if Employments.objects.filter(filters).exists():
context['info'] = 'retired employee'
return context
This works without any problems. But how to deal with the use case, that i want have all recorded employments generelly filtered for retired employees?
I tried it with:
email_queryset = Employees.objects.all().order_by('last_name').values().distinct()
......
if 'retired' in request.POST:
retired = request.POST['retired']
context['retired'] = retired
if retired == "only retired":
filters &= Q(employments__duty_end__lt=datetime.date.today()) & Q(employments__keep_data=1)
elif retired == "with retired":
filters &= ~Q(Q(employments__duty_end__lt=datetime.date.today()) & Q(employments__keep_data=0))
context['emails'] = email_queryset.filter(filters)
Which basically works, but fails in all cases, where an employee has more than one employment, because only the last one is active and should be chosen for the query, but of course, with this filters, all entries are looked up and so he will be dealt like retired if one of the former employments match the query. So I am looking for a way to get the latest entry of n employments per employee but i don`t know how if i have no specific id for a single dataset. …