order_with_respect_to in admin interface

I’m implementing a model with order_with_respect_to as described in documentation

I’m trying to implement that in the admin interface for showing the order and also for editing. I’m thinking about creating a calculated field to show the information and I’m not sure how to implement the form for editing. Is that correct? any suggestion?

Can you clarify what you’re trying to accomplish here? When you say that you’re thinking about creating a calculated field to show the information, what information are you looking to show? (And how does “order_with_respect_to” fit into this?)
Also, “implement the form for editing” - what are you looking to edit?

Sure. I need to implement a relationship and maintain that relationship with a custom order and be able to modify that order so I have achieved that relationship with order_with_respect_to
Here is my model:

class Game(models.Model):
    title = models.CharField(max_length=255,null=False, blank=False)
    description = models.TextField(null=True, blank=True)
    [...]
class Task(models.Model):
    task_id = models.IntegerField(primary_key=True, editable = False,)
    game = models.ForeignKey(Game, on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    description = models.TextField()
    [...]
    class Meta:
        order_with_respect_to = 'game'

I can change order manually

a=Game.objects.get(id=15)    
a.get_task_order()
<QuerySet [1, 2, 3]>
a.set_task_order([3,2,1])
a.get_task_order()
<QuerySet [3, 2, 1]>

I would like to be able to do the same in the admin interface (view and modify the relationship order).
I have read ModelAdmin documentation but I don’t know where to start

In terms of database design, relationships have no intrinsic order. What’s important is how it’s retrieved when needed.

I guess I’m still not seeing what you’re trying to accomplish, and would like to understand the base requirement here. Is it that you want to provide two different views where each view shows these lists in different orders? Or are you looking for something where a person can select the order in which the list is displayed? Or are you saying that these lists need to be display in some arbitrary order?
(I don’t know what these get_task_order or set_task_order function are or what they do.)

(Quite honestly, I’m getting the feeling that there may be a bit of an “XY Problem” here.)

I’m sorry not to be clear I’ll try to explain in other words.

I have games and each Game contain multiple puzzles (Task). Those tasks must be placed in a given order so the player is passing all tasks of a game in the established order.

I configure the game and the tasks in the admin interface. The order is as they are created for each game so let say I have Game1 with the following tasks in order [Taks1, Task3, Taks8]

After testing a game I decided to add a new Task (example Task10) and creting a new Taks for that Game, the order will be consecutive [Taks1, Task3, Taks8, Task10] but I would like to reorder for example like this: [Taks1, Task10, Task3, Taks8]

Even in terms of database design relationship haven’t intrinsic order, django provide a way to do that using class Meta option order_with_respect_to in your model:

Makes this object orderable with respect to the given field, usually a ForeignKey . This can be used to make related objects orderable with respect to a parent object.

And regarding that methods, djanjo automatically defines them:

When order_with_respect_to is set, two additional methods are provided to retrieve and to set the order of the related objects: get_RELATED_order() and set_RELATED_order() , where RELATED is the lowercased model name.

I would like to be able to modify that order in the admin interface. Django provides methods to reorder the relationship but there is no information about how to implement that in the admin interface like any other field to show or edit. It would be great a specific widget to reorder a list. So I don’t know how to implement the possibility to reorder that in django admin interface.

Please, let me know if still is not clear. English is not my mother tongue and sometimes it’s a little bit hard to me express complex explanations.

We’re all good now, thanks. That helps a lot!

What you’re looking for then is another field in your tasks to identify the sequence number.

example:

class Task(models.Model):
    task_id = models.IntegerField(primary_key=True, editable = False,)
    game = models.ForeignKey(Game, on_delete=models.CASCADE)
    task_order = models.IntegerField()
    title = models.CharField(max_length=255)
    description = models.TextField()
    [...]
    class Meta:
        order_with_respect_to = 'game'
        unique_together = ['game', 'task_order']

In your view, you can set the task_order value to the index number of the task in your list.

Then, you can either set ordering to task_order to make it the default sort, or you use the order_by method on your queries when it’s needed.

For being able to edit this within the admin, you’re either going to need to craft your own solution, or pick a third-party package to help. I don’t believe there’s anything in Django core to facilitate this. There are some packages available - I haven’t used any so I don’t know how well they work or whether or not they fit your requirements.

In general terms, when I’m looking for third party packages, I start with djangopackages.org. For your particular case, I’d look at either Model Ordering or possibly Admin Interface.