Salve a tutti, vi spiego un problema riscontrato in fase di ordinamento , aiutatemi a capire...
Prendo un modello django class Work(models.Model): shootingDate = models.DateField(null=True, blank=True,editable=True) ….. ….. Di seguito userò values_list(‘pk’ , flat=True) per facilita di lettura, ma rimuovendolo il problema persiste. QuerySet per ottenere tutti gli oggetti >>> print Work.objects.all().values_list('pk',flat=True).query SELECT "main_work"."id" FROM “main_work" >>> Work.objects.all().values_list('pk',flat=True) [18, 32, 33, 8, 4, 23, 19, 9, 20, 10, 11, 16, 15, 24, 27, 26, 17, 21, 34, 25, '...(remaining elements truncated)…'] Se prendo i prim 5 il risultato che ottengo equivale a quello atteso. ossia >>> Work.objects.all().values_list('id',flat=True)[0:5] [18, 32, 33, 8, 4] Applicando invece un ordinamento, sorgono i miei problemi: ordino la QuerySet per “-shootingDate” >>> print >>> Work.objects.all().order_by('-shootingDate').values_list('id',flat=True).query SELECT "main_work"."id" FROM "main_work" ORDER BY "main_work"."shootingDate” DESC >>> Work.objects.all().order_by('-shootingDate').values_list('id',flat=True) [31, 32, 33, 23, 19, 20, 24, 26, 17, 21, 25, 22, 30, 29, 18, 13, 11, 12, 4, 14, '...(remaining elements truncated)...'] Chiedendo i primi 5 elementi e questo e’ il risultato: >>> Work.objects.all().order_by('-shootingDate').values_list('id',flat=True)[0:5] [23, 33, 32, 18, 19] io mi aspettavo [31,32,33,23,19] Se ne chiedo 10 l’ordine cambia ancora. >>> Work.objects.all().order_by('-shootingDate').values_list('id',flat=True)[0:10] [17, 33, 24, 26, 23, 19, 32, 20, 18, 21] Questo e' problema mi si e’ presentato in una class Paginator che eseguiva questo metodo: def get_slice(self, limit, offset): """ Slices the result set to the specified ``limit`` & ``offset``. """ if limit == 0: return self.objects[offset:] return self.objects[offset:offset + limit] poi provando da shell ho replicato il problema. La cosa strana e’ che se invece accedo agli oggetti richiesti alla QuerySet anche con un semplice print, il problema svanisce e il risultato dell’operazione di slice restituisce il risultato aspettato . Ovviamente questo vorrei evitarlo: >>> sorted_objects = >>> Work.objects.all().order_by('-shootingDate').values_list('id',flat=True) >>> sorted_objects [31, 32, 33, 23, 19, 20, 24, 26, 17, 21, 25, 22, 30, 29, 18, 13, 11, 12, 4, 14, '...(remaining elements truncated)...'] >>> sorted_objects[0:5] [23, 33, 32, 18, 19] >>> print [id for id in sorted_objects] [31, 32, 33, 23, 19, 20, 24, 26, 17, 21, 25, 22, 30, 29, 18, 13, 11, 12, 4, 14, 15, 16, 34, 5, 35, 36, 8, 7, 6, 3, 10, 9, 28, 27] >>> sorted_objects[0:5] [31, 32, 33, 23, 19] >>> Tutto questo su DB e’ postgres 9.3 Django 1.6.6 python 2.7.8 C’e’ qualcosa che non tengo in considerazione? Succede solo a me o qualcun’altra riesce a replicare il problema? Saluti --- Paolo Leggio Cel. +39.349.3420555 | Tel. +39.02.87244015 www.digitalmonkeys.it | pa...@digitalmonkeys.it
_______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python