Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse order when using _set.all

I have two relationships:

class Contact(models.Model):
    first_name  =models.CharField(max_length=30)

class Activity(models.Model):
    action      =models.CharField(max_length=200)
    whom        =models.ForeignKey("Contact", null=True, blank=True)

With

 contacts=Contact.objects.get(slug=contactslug)

I can call the specific Contant from my url request

And additionally

c = contacts.activity_set.all

lets me call all the activities.

How can I call the activities but in reverse order since .order_by() or reverse() doesn't seem to work.

like image 279
Patrick tran Avatar asked Jul 13 '12 06:07

Patrick tran


1 Answers

This will probably work

reversed(contacts.activity_set.all())

But, you should do this:

class Activity(models.Model):
    action      =models.CharField(max_length=200)
    whom        =models.ForeignKey("Contact", null=True, blank=True)

    class Meta:
        ordering = ['action']

or ['-action']

like image 163
Hedde van der Heide Avatar answered Oct 11 '22 13:10

Hedde van der Heide