I have a model:
class Person(models.Model):
name = models.CharField(...)
def __unicode__(self):
return u'%s' % self.name
class Entity(models.Model):
client = models.ForeignKey(Person)
In my view I just want to obtain all the Entities and order them by the name of the client:
def all_entities(request):
entities = Entity.objects.all().order_by('client')
return render(request, 'template.html', {'entities': entities})
And so my template just prints all the entities.
<html>
{% for entity on entities %}
{{ entity.client }}}
{% endfor %}
</html>
Happens is that in my development PC all works pretty good, I get a list of ordered client names, what I can't understand is that on my production server on webfaction.com the result is not ordered alphabetically as in my local environment.
I testes it on the shell too, results are the same, somehow order_by('client') is returning an alphabetically sorted list on my PC but not alphabetically ordered on production.
Notes: Both environments have Postgres and use the same backend.
Thanks
Well, I'm not going to try to figure out why it appears to work for you, but I can't help but notice that the ORM wouldn't know which field to order by given a direct order to order by client
.
It's probably just sorting by PK.
entities = Entity.objects.all().order_by('client__name')
# ^^^^^^ specify sort field
# I am addicted to carets