为什么我的 Django 从 Postgres 数据库中查询 return 查询设置为相反的顺序
Why does my Django query from Postgres database return the query set in reverse order
我有一个名为“Step”的模型,它通过外键连接到学习指南。在我的 views.py 文件中,我只是使用这个查询步骤
learning_guides_steps = learning_guide.step_set.all()
在模板中,我只是使用 for 循环遍历这些步骤,并像这样显示步骤的标题
{%for step in learning_guides_steps%}
<div class="entire_step">
<!-- Displaying the title of the step with an angle down button beside the title -->
<div class = "btn-group step" >
<a href = "#" class = "btn stepname" role = "button" style = "font-size: 18px;"> {{forloop.counter}}. {{step.title}}</a>
</div>
</div>
然而,当我在网站上显示它时,它的打印顺序是相反的。谁能解释为什么会这样以及我该如何解决这个问题?我是 Django 和 Postgres 的新手,所以任何帮助都会很感激。谢谢你。我的 Step 模型和视图如下
class Step(models.Model):
title = models.CharField(max_length=200, null=False)
step_description = models.TextField(max_length=1000, null=False)
# enforces one-to-many relationship between LearningGuide and Step.
# Deleting a LearningGuide deletes all the associated steps (CASCADE delete)
learning_guide = models.ForeignKey(LearningGuide, on_delete=models.CASCADE)
class LearningGuide(models.Model):
title = models.CharField(null=False, max_length=50)
image = models.ImageField(upload_to='learning_guides_images/')
short_description = models.TextField(max_length=100, null=False)
category = models.ForeignKey(Category, on_delete=models.DO_NOTHING)
indicators = models.ManyToManyField(Indicator, blank=True)
external_resource = models.BooleanField(default=False, blank=True)
external_resource_link = models.URLField(null=True, blank=True)
我的 views.py 文件:
learning_guide = LearningGuide.objects.get(pk=learning_guide_id)
learning_guides_indicators = learning_guide.indicators.all()
indicators = Indicator.objects.all()
learning_guides_steps = learning_guide.step_set.all()
context = {
'learning_guide': learning_guide,
'indicators': indicators,
'learning_guides_indicators':learning_guides_indicators,
'learning_guides_steps':learning_guides_steps,
'homepage':False
}
选项 1(可能是最简单的)
使用 order_by:
在您的视图中对查询结果进行排序
learning_guides_steps = learning_guide.step_set.all().order_by('step__title')
# OR to order in the opposite direction,
learning_guides_steps = learning_guide.step_set.all().order_by('-step__title')
请注意,我猜你想按步骤的标题排序,但你可以通过替换 order_by 中的标题按任何其他字段排序,例如 order_by('step__pk')
如果你想按步骤的主键排序。请注意,在每种情况下 -
都会反转方向。并注意步骤和标题之间的双下划线 step__title
因为您是按外键排序的。
选项 2
使用 class Meta:
设置模型本身的默认顺序
class Step(models.Model):
title = models.CharField(max_length=200, null=False)
step_description = models.TextField(max_length=1000, null=False)
learning_guide = models.ForeignKey(LearningGuide, on_delete=models.CASCADE)
class Meta:
ordering = ['title']
# OR in the other direction
ordering = ['-title']
这将需要您 运行 python manage.py makemigrations
和 python manage.py migrate
选项 3
向步骤添加一个订单字段,并按此进行排序。这样您就可以设置顺序,而不是按字母顺序或自动生成的主键排序:
class Step(models.Model):
title = models.CharField(max_length=200, null=False)
order = models.PositiveIntegerField(default=1)
step_description = models.TextField(max_length=1000, null=False)
learning_guide = models.ForeignKey(LearningGuide, on_delete=models.CASCADE)
class Meta:
ordering = ['order']
ordering = ['-order']
请注意,第三个选项还需要您 运行 python manage.py makemigrations
和 python manage.py migrate
。我已将默认值 1 添加到订单中,以便此迁移不会抱怨 NULL 值,但这意味着在您更改之前所有步骤的订单都将是 1。有办法解决这个问题,但我认为这会奏效。
我有一个名为“Step”的模型,它通过外键连接到学习指南。在我的 views.py 文件中,我只是使用这个查询步骤
learning_guides_steps = learning_guide.step_set.all()
在模板中,我只是使用 for 循环遍历这些步骤,并像这样显示步骤的标题
{%for step in learning_guides_steps%}
<div class="entire_step">
<!-- Displaying the title of the step with an angle down button beside the title -->
<div class = "btn-group step" >
<a href = "#" class = "btn stepname" role = "button" style = "font-size: 18px;"> {{forloop.counter}}. {{step.title}}</a>
</div>
</div>
然而,当我在网站上显示它时,它的打印顺序是相反的。谁能解释为什么会这样以及我该如何解决这个问题?我是 Django 和 Postgres 的新手,所以任何帮助都会很感激。谢谢你。我的 Step 模型和视图如下
class Step(models.Model):
title = models.CharField(max_length=200, null=False)
step_description = models.TextField(max_length=1000, null=False)
# enforces one-to-many relationship between LearningGuide and Step.
# Deleting a LearningGuide deletes all the associated steps (CASCADE delete)
learning_guide = models.ForeignKey(LearningGuide, on_delete=models.CASCADE)
class LearningGuide(models.Model):
title = models.CharField(null=False, max_length=50)
image = models.ImageField(upload_to='learning_guides_images/')
short_description = models.TextField(max_length=100, null=False)
category = models.ForeignKey(Category, on_delete=models.DO_NOTHING)
indicators = models.ManyToManyField(Indicator, blank=True)
external_resource = models.BooleanField(default=False, blank=True)
external_resource_link = models.URLField(null=True, blank=True)
我的 views.py 文件:
learning_guide = LearningGuide.objects.get(pk=learning_guide_id)
learning_guides_indicators = learning_guide.indicators.all()
indicators = Indicator.objects.all()
learning_guides_steps = learning_guide.step_set.all()
context = {
'learning_guide': learning_guide,
'indicators': indicators,
'learning_guides_indicators':learning_guides_indicators,
'learning_guides_steps':learning_guides_steps,
'homepage':False
}
选项 1(可能是最简单的)
使用 order_by:
learning_guides_steps = learning_guide.step_set.all().order_by('step__title')
# OR to order in the opposite direction,
learning_guides_steps = learning_guide.step_set.all().order_by('-step__title')
请注意,我猜你想按步骤的标题排序,但你可以通过替换 order_by 中的标题按任何其他字段排序,例如 order_by('step__pk')
如果你想按步骤的主键排序。请注意,在每种情况下 -
都会反转方向。并注意步骤和标题之间的双下划线 step__title
因为您是按外键排序的。
选项 2 使用 class Meta:
设置模型本身的默认顺序class Step(models.Model):
title = models.CharField(max_length=200, null=False)
step_description = models.TextField(max_length=1000, null=False)
learning_guide = models.ForeignKey(LearningGuide, on_delete=models.CASCADE)
class Meta:
ordering = ['title']
# OR in the other direction
ordering = ['-title']
这将需要您 运行 python manage.py makemigrations
和 python manage.py migrate
选项 3
向步骤添加一个订单字段,并按此进行排序。这样您就可以设置顺序,而不是按字母顺序或自动生成的主键排序:
class Step(models.Model):
title = models.CharField(max_length=200, null=False)
order = models.PositiveIntegerField(default=1)
step_description = models.TextField(max_length=1000, null=False)
learning_guide = models.ForeignKey(LearningGuide, on_delete=models.CASCADE)
class Meta:
ordering = ['order']
ordering = ['-order']
请注意,第三个选项还需要您 运行 python manage.py makemigrations
和 python manage.py migrate
。我已将默认值 1 添加到订单中,以便此迁移不会抱怨 NULL 值,但这意味着在您更改之前所有步骤的订单都将是 1。有办法解决这个问题,但我认为这会奏效。