"id" field - django models

hello,
I want to implement order in my application.
I have items in a “shop” that admin can add and change in the admin menu, the items are displayed one by one with no order ATM. I want to add order field which will be an intgerer with a deaflut value of (amount of items +1) how can i implement this?

this question leads me to the next one, say I want to display how many users I have, I can do
len (User.objects.all()) which will give me the number but wil be very ineffiecnt since I querry all the information I have just to recive a number. is there any other way of doing so?
thanks!

See the count() method.

Are these items always going to be in the same order? Or is there the (known or perceived) requirement to allow someone to re-order those items?

allow someone to reorder.

For reordering, you’ll want to consider the complete set of elements as a single “unit”, where you select that unit using the select_for_update clause to lock those roles while you’re working with / updating them.

You’ll also need to consider what the chances are that someone is adding an entry to that list while someone else is reordering it - or that two people are adding entries at the same time. Either situation can create a race condition potentially creating an error.

since only admins can update the order my idea was to set a field to each item (integer) that starts at 0 and counts up. and when admin for instance swap item 1 with 5 i just swap the orders.

isn’t it a good fix? and if so how can i make the deaflut value the amount of items ?
thanks!

If I were doing this, I wouldn’t care what the actual number was being used as the sequence number. I would initialize all new entries with being equal to the id of the table. (That way, it’s always going to be the highest current value.) There is no benefit to the process by trying to force that sequence to initially be a set of consecutive and contiguous integers.

That list is then sortable and represent the proper sequence.

When the list is manually resequenced, then I would accept values from 1 to n, where n is the number of values in the list.