在这里,我尝试了这样的操作,但它没有给我正确的顺序。class VAttribute(models.Model): attribute = models.ForeignKey(PAttribute, on_delete=models.CASCADE)....class PAttribute(models.Model): name = models.CharField(max_length=255, unique=True)from operator import attrgetterqs = VAttribute.objects.all()ls = [obj.attribute for obj in qs]#ls = [<PAttribute: name1>, <PAttribute: nam2>]ordered_ls = sorted(ls, key=attrgetter('name'))
1 回答
慕少森
TA贡献2019条经验 获得超9个赞
只需调用order_by()
为
qs_oder_by_name = VAttribute.objects.order_by("attribute__name")
添加回答
举报
0/150
提交
取消