﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13442	only() and defer() will always select id	mampf		"Hi there,

when you do a query with defer() or only(), you will still execute a SELECT query including the object's id. This makes it impossible to use distinct() on it.
{{{
#!python
last_5_uploaded_files = UploadFile.objects.order_by('upload_time')[:5] 
last_changed_list = MainObject.objects \ 
    .filter(upload_file__in=last_5_uploaded_files) \ 
    .only('field_1', 'hostname', 'target', 'modifed_date') \
    .distinct() \ 
    .order_by('-modified_date')[:50]

    response = render_to_response('output/mainobject_list.html',	
    {'object_list': last_changed_list,})

    connection.queries
    print("""")
    # Still includes MainObject.id! WHY?
    print(""current queue: %s"" % connection.queries)
    print("""")

    return response
}}}
Now if I print the executed SQL using connection.queries I get an SQL statement which includes queryset.id. This is why my distinct won't word - I want a distinct list created without id.[[BR]]
On the other hand, '''.values('field_1', 'field_n')''' does work. But this way I do not get the foreign table resolved, just it's id.

Please change only() and defer(), so it doesn't select the id in any case.

Thanks

"		closed	Database layer (models, ORM)	1.1		invalid			Unreviewed	0	0	0	0	0	0
