0

I'm trying to add an extra field to the instances of my queryset and then sort the set by the new field. But I get a field error( Cannot resolve keyword 'foo' into field. Choices are: ... )

My view(abstract):

def view(request):
    instances = Model.objects.all()
    for counter, instance in enumerate(instances):
        instance.foo = 'bar' + str(counter)
    instances.order_by('foo')  #this line causes trouble
    return render(request, 'template.html', {'instances': instance})

My template:

{% for instance in instances %}
    {{instance.foo}}
{% endfor %}

If I leave out the order_by line the templates renders as expected, so the field seems to be there.

So why do I get a field error?
It would be awesome, if somebody could help me to understand what I'm doing wrong.

Thanks in advance.


I found a possible solution to change the template to

{% for instance in instances|dictsort:'foo' %}

and that works fine, but from what I understand there should be as little logic as possible in the view, so I figure sorting should be done in the view.
Or is this actually the right way?

3
  • Because it is not a field, you added an extra attribute to the objects. But that does not matter. A queryset deals with a query in the first place. If you call .order_by you construct a new QuerySet on that model. Since foo is not a field, you can not sort on this. Commented Aug 6, 2018 at 14:33
  • Thanks for the answer, that clarifies it alot. So order_by actually results in another call to the database and of course the field isn't there, so I get the error. How would I sort by the new attribute? - Edit: Just saw your answer, that explains it.
    – HahaMuntz
    Commented Aug 6, 2018 at 14:41
  • with a Python sort function, like sorted(..) for example. Commented Aug 6, 2018 at 14:42

2 Answers 2

2

The Django ORM aims to construct database queries. As a result, you can only query on what a database "knows". Methods, properties, or attributes you added yourself are unknown to the database. The .order_by thus has no effect, since you "patched" the objects in the instances queryset.

If you however call an instances.order_by you construct a new queryset. This queryset takes the context of the parent, and thus represents a (slightly) modified query, but again, a query. Whether the old queryset is already evaluated or patched, is of no importance.

Furthermore even if there was a column foo, it would not help, since the instance.order_by does not order the instance queryset, it constructs a new one, one that looks approximately like the old one, except that the rows are ordered.

You thus will have to sort in Python now. You can for example construct a list of ordered elements with sorted(..), like:

from operator import attrgetter

def view(request):
    instances = Model.objects.all()
    for counter, instance in enumerate(instances):
        instance.foo = 'bar' + str(counter)
    mydata = sorted(instances, key=attrgetter('foo'))
    return render(request, 'template.html', {'instances': mydata})

So now mydata is no longer a QuerySet, but a vanilla list. Furthermore note that ordering in a database might be slightly different than ordering in Python. In Python exceptions can occur if not all elements have the same type, and furthermore it is not guaranteed that the semantics behind the order relation is exactly the same.

2
  • That works, thanks for that. I have now implemented that everywhere in my code, but when I try that to sort the options of a ManyToMany form field I get an attribute error( 'list' object has no attribute 'all'). I try to limit the choices with form.fields['instances'].querset=mydata . Do you have an idea how to achieve that?
    – HahaMuntz
    Commented Aug 6, 2018 at 15:41
  • @HahaMuntz: in case a queryset is necessary, then you can not do this. Then you probably better add an extra foo field to the database, and update it regularly with the calculations. Commented Aug 6, 2018 at 17:55
0

The new attribute in the Python Objects does not exist in the database and only in those instances. order_by changes the queryset and not your current list of objects stored in memory.

One approach would be to use the builtin python sorting functions in the view like: sorted or even list.sort().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.