I wrote this:
class Department(models.Model):
...
sort_order = models.IntegerField(help_text="Leave negative to place at end", default=-1)
def save(self, *args, **kwargs):
""" Set the sort order (if unset) to larger than the largest value"""
if self.sort_order <= 0:
largest = Department.objects.all().aggregate(x = models.Max('sort_order'))['x'] or 0
self.sort_order = largest + 10
super(Department, self).save(*args, **kwargs)
but I'm not proud of it. Is there a better way to do this? I could possibly roll this into a field, but not sure how I'd get back to the model table if I refactored the code there. I can't use the autoincrement column because as FK values, those can't be changed.