Answer the question
In order to leave comments, you need to log in
How to sort queryset by related OneToOne fields on the same table?
I'm adding something like an embedded trello to my project. You need to link the positions of the elements on the front with the positions of the elements on the back.
The models look like this:
class Board(models.Model):
name = models.CharField(verbose_name="Название", max_length=200)
user = models.ForeignKey(AUTH_USER_MODEL, verbose_name="Пользователь", on_delete=models.CASCADE)
class Meta:
verbose_name = "Доска"
verbose_name_plural = "Доски"
def __str__(self):
return self.name
class Column(models.Model):
name = models.CharField(verbose_name="Название", max_length=200)
board = models.ForeignKey("canban.Board", verbose_name="Доска", on_delete=models.CASCADE)
next = models.OneToOneField("canban.Column", on_delete=models.SET_NULL, null=True, default=None, related_name="previous")
class Meta:
verbose_name = "Колонка"
verbose_name_plural = "Колонки"
def __str__(self):
return self.name
class Task(models.Model):
title = models.CharField(verbose_name="Название", max_length=200)
column = models.ManyToManyField("canban.Column")
next = models.OneToOneField("canban.Task", on_delete=models.SET_NULL, null=True, default=None, related_name="previous")
class Meta:
verbose_name = "Задача"
verbose_name_plural = "Задачи"
def __str__(self):
return self.title
next
and previous
. previous
, and the last one will be the one that does not have next
. It is also clear that the position of each element is the number of calls to it previous
until previous
it becomes null
. next
or previous
any element at any time to get its position as an integer in order to annotate it and sort the queryset by it. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question