I need to preload the update history queries saved with django-simple-history, to significantly improve the query execution time, for example, I have these models in my application:
class Staff(models.Model):
registration = models.CharField(max_length=100, unique=True)
identification_type = models.CharField(max_length=2, choices=(
('V', 'V'),
('E', 'E'),
('J', 'J')
), default='V')
identification_number = models.CharField(max_length=100, unique=True)
first_names = models.CharField(max_length=100)
last_names = models.CharField(max_length=100)
birth_date = models.DateField()
hire_date = models.DateField()
hire_date_to_public_administration = models.SmallIntegerField(default=0)
termination_date = models.DateField(null=True)
sex = models.CharField(max_length=10, choices=(
('F', 'Femenino'),
('M', 'Masculino')
))
address = models.TextField()
marital_status = models.CharField(max_length=15, choices=(
('Soltero/a', 'Soltero/a'),
('Casado/a', 'Casado/a'),
('Divorciado/a', 'Divorciado/a')
))
education_level = models.CharField(max_length=100)
department = models.ForeignKey(
Department,
on_delete=models.RESTRICT,
related_name='staffs'
)
position = models.ForeignKey(
Position,
on_delete=models.RESTRICT,
related_name='staffs'
)
payroll_type = models.CharField(max_length=10, choices=[
('Empleado', 'Empleado'),
('Obrero', 'Obrero'),
], default='Empleado')
worker_type = models.CharField(max_length=10, choices=[
('Fijo', 'Fijo'),
('Contratado', 'Contratado'),
('Pensionado', 'Pensionado'),
('Jubilado', 'Jubilado'),
], default='Contratado')
status = models.CharField(max_length=100, choices=(
('Activo', 'Activo'),
('Inactivo', 'Inactivo'),
), default='Activo')
bank = models.CharField(max_length=100)
bank_number = models.CharField(max_length=100)
concepts = models.ManyToManyField(
Concept,
related_name='staffs',
blank=True
)
salary = models.DecimalField(max_digits=8, decimal_places=2)
children_number = models.SmallIntegerField(default=0)
is_disabled = models.BooleanField(default=False)
receive_complement = models.BooleanField(default=False)
history = HistoricalRecords()
class Meta:
ordering = ['identification_number']
class Payment(models.Model):
date = models.ForeignKey(
Calendar,
on_delete=models.RESTRICT,
related_name="payments"
)
staff = models.ForeignKey(
Staff,
on_delete=models.RESTRICT
)
total = models.DecimalField(max_digits=8, decimal_places=2, default=0)
total_deduction = models.DecimalField(max_digits=8, decimal_places=2, default=0)
total_allowance = models.DecimalField(max_digits=8, decimal_places=2, default=0)
paid_concepts = models.ManyToManyField('PaidConcept')
create_at = models.DateTimeField(auto_now_add=True)
I am trying to make a query to obtain the payments for a given date.
payments = (
Payment.objects.filter(date__id=week.id, staff__worker_type=worker_type)
.select_related('staff', 'staff__department')
.order_by('staff__last_names')
)
But for each payment, I am making an extra request to get the worker’s information at the time the payment was made, which drastically decreases the performance of the query.
for payment in payments:
historical_staff_data = payment.staff.history.as_of(payment.create_at)
data.append({
'staff_registration': historical_staff_data.registration,
'staff_identification_number': historical_staff_data.identification_number,
'staff_first_names': historical_staff_data.first_names,
'staff_last_names': historical_staff_data.last_names,
'staff_bank_number': historical_staff_data.bank_number,
'total': payment.total
})
I want to know if it is possible to preload the information of the history, as it is being done with the worker’s department or even with the worker himself.
I tried to do it as explained in the django-simple-history documentation, but it didn’t work.
NSMichelJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.