Django comes with a complete collection of model fields that you can use to build your models. However, you can also create your own model fields to store custom data or alter the behavior of existing fields.
We need a field that allows us to specify an order for objects. If you think about an easy way to do this with a field provided by Django, you will probably think of adding a PositiveIntegerField
to your models. This is a good starting point. We can create a custom field that inherits from PositiveIntegerField
and provides additional behavior.
There are two relevant functionalities that we will build into our order field:
Automatically assign an order value when no specific order is provided. When no order is provided while storing an object, our field should automatically assign the next order based on the last existing ordered object. If there are two objects with order
1
and2
respectively, when saving a third object, we should automatically assign the order...