You are not logged in.

Change Language:

Freelancer? Consultant? Check out: WorkTrail - Time Tracking made easy
Board » Django » Models » [Queryset] Sorting only the children

Hello everyone,

I'm new to django and to this forum btw.

I am developing a very simple application to handle my resume.

So there is only two models so far : "Category", and "Experience"
Experience has a foreign key to Category.

And I would like to retrieve in a view all the Category with their experiences but would like to sort the experience by the field "end_date".

So I tried many ways like :

>>> Category.objects.all().order_by( 'experience__end_date' ).distinct()
[<Category: Compétences>, <Category: Centre d'intérêts>, <Category: Expériences>, <Category: Formation>, <Category: Expériences>, <Category: Formation>, <Category: Expériences>, <Category: Expériences>, <Category: Expériences>, <Category: Formation>, <Category: Formation>]

>>> Category.objects.all().order_by( 'experience__end_date' ).distinct( 'name' )
[<Category: Compétences>, <Category: Centre d'intérêts>, <Category: Expériences>, <Category: Formation>, <Category: Expériences>, <Category: Formation>, <Category: Expériences>, <Category: Expériences>, <Category: Expériences>, <Category: Formation>, <Category: Formation>]

>>> Category.objects.all().order_by( 'experience__end_date' ).distinct( 'experience__end_date' )
[<Category: Compétences>, <Category: Centre d'intérêts>, <Category: Expériences>, <Category: Formation>, <Category: Expériences>, <Category: Formation>, <Category: Expériences>, <Category: Expériences>, <Category: Expériences>, <Category: Formation>, <Category: Formation>]

But it returns me too many parents !

If you could give me some advice ?

Thank you


A sample of my code if you want : http://pastebin.com/JrawK388
I've found the solution.

I have to put a class Meta on the models for my experience, with ordering = ['-end_date'].

If anyone need this solution... share it





Powered by Sphene Community Tools