在這里,我嘗試了這樣的操作,但它沒(méi)有給我正確的順序。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貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
只需調(diào)用order_by()
為
qs_oder_by_name = VAttribute.objects.order_by("attribute__name")
添加回答
舉報(bào)
0/150
提交
取消