在这里,我尝试了这样的操作,但它没有给我正确的顺序。
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 attrgetter
qs = VAttribute.objects.all()
ls = [obj.attribute for obj in qs]
#ls = [<PAttribute: name1>, <PAttribute: nam2>]
ordered_ls = sorted(ls, key=attrgetter('name'))
慕少森
相关分类