def recalculate_abs_length(apps, schema_editor): """ Recalculate absolute lengths for all cables with a length and length unit defined. Fixes incorrectly calculated values as reported under bug #8377. """ Cable = apps.get_model('dcim', 'Cable') cables = Cable.objects.filter(length__isnull=False).exclude(length_unit='') for cable in cables: cable._abs_length = to_meters(cable.length, cable.length_unit) Cable.objects.bulk_update(cables, ['_abs_length'], batch_size=100)
def save(self, *args, **kwargs): # Store the given length (if any) in meters for use in database ordering if self.length and self.length_unit: self._abs_length = to_meters(self.length, self.length_unit) else: self._abs_length = None # Store the parent Device for the A and B terminations (if applicable) to enable filtering if hasattr(self.termination_a, 'device'): self._termination_a_device = self.termination_a.device if hasattr(self.termination_b, 'device'): self._termination_b_device = self.termination_b.device super().save(*args, **kwargs) # Update the private pk used in __str__ in case this is a new object (i.e. just got its pk) self._pk = self.pk