[Django]-Relative incremental ID by reference field

0👍

I hate always being the one that responds its own questions, but I solved using this:

class Reservation(models.Model):

    # ...

    def relative_id(self):
        return self.id - Reservation.objects.filter(id__lt=self.id).filter(~Q(event=self.event)).all().count()

Assuming records from reservations are never deleted, we can safely assume the “relative id” is the incremental id – (count of reservations before this one not belonging to same event).

I’m thinking of any drawbacks, but I didn’t find any.

0👍

Filtering using Reservation.objects.filter(event_id = some_event_id) should suffice. This will give you a QuerySet that should have the same ordering each time. Or am I missing something in your question?

Leave a comment