"id" field - django models

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?

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 ?

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.

I have a question regarding ordered models.
basicly, i have course model, each course has many sections and each section has many lectures.
I want to make all the sections in a course ordered.
for example
course one → section1 , section 2 , section 3 , section 4
course two → section 3 , section 1 , section 4 , section 2.
what is the correct way to use the order_with_respect_to field?

this is the libary GitHub - django-ordered-model/django-ordered-model: Get your Django models in order.

thanks alot!

The simple example from the docs seems to match what you’re describing:

class Contact(OrderedModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    phone = models.CharField()
    order_with_respect_to = 'user'

In your case it’d be

class Section(OrderedModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    order_with_respect_to = 'course'