Use Subset Ordering with `order_with_respect_to`
masterIf you need to order objects within specific groups (e.g., contacts belonging to different users), use the order_with_respect_to attribute. This ensures that the ordering of one group does not affect the ordering of another.
Single Field
Set order_with_respect_to to the name of a ForeignKey field.
Multiple Fields
Pass a tuple of field names to define ordering across multiple dimensions.
Related Model Fields
You can use the double-underscore syntax to specify a field on a related model (e.g., 'group__user').
Note: order_with_respect_to must specify a ForeignKey field on the model, otherwise a Django Check error (E002, E005, or E006) will be raised.
class Contact(OrderedModel):
user = models.ForeignKey(User, on_delete=models.CASCADE)
phone = models.CharField()
order_with_respect_to = 'user'
# Multiple fields
class Model(OrderedModel):
order_with_respect_to = ('field_a', 'field_b')
# Related model field
class GroupedItem(OrderedModel):
group = models.ForeignKey(ItemGroup, on_delete=models.CASCADE)
order_with_respect_to = 'group__user'