Adam
Adam

Reputation: 3128

Django model ordering issue

This is my model:

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']

The "Features" are being correctly display in the admin control panel based on the value specified in "order".

I have this in my view:

p = get_object_or_404(Phone.objects.prefetch_related('brand', 'features__feature', 'photo_set'), id=id)

I templates I have {% for feature in phone.features.all %}... {{ feature.feature }} ...

The values are being displayed correctly but in random order.

What's wrong and how can I overcome this problem?

Thanks.

Upvotes: 0

Views: 87

Answers (1)

Fiver
Fiver

Reputation: 10167

How about the template filter dictsort

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#dictsort

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

Upvotes: 1

Related Questions