Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django模型排序问题_Python_Django - Fatal编程技术网

Python Django模型排序问题

Python Django模型排序问题,python,django,Python,Django,这是我的模型: class Feature(models.Model): name = models.CharField(max_length=75, blank=True) order = models.SmallIntegerField() group = models.ForeignKey(FeatureGroup) def __unicode__(self): return self.name

这是我的模型:

class Feature(models.Model):
    name         = models.CharField(max_length=75, blank=True)
    order        = models.SmallIntegerField()
    group        = models.ForeignKey(FeatureGroup)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['order']
根据“订单”中指定的值,“功能”将正确显示在管理控制面板中

我认为:

p = get_object_or_404(Phone.objects.prefetch_related('brand', 'features__feature', 'photo_set'), id=id)
我在phone.features.all%}中有
{%的功能模板。。。{{feature.feature}}…

数值显示正确,但顺序随机

出了什么问题?我如何克服这个问题


谢谢。

模板过滤器如何
dictsort


{%for feature in phone.features.all | dictsort:“order”%}

在您的视图中,您似乎将
p
分配给单个对象,通过
id
检索它。单个元素将没有排序顺序,或者您需要按照用于检索的
id
s进行排序。