Say we have:

class Foo(models.Model):
     title = models.CharField(max_length=128)
     number = models.PositiveSmallIntegerField()
     bar = models.ForeignKey('Bar')

     class Meta:
         ordering = ['title',]

class Bar(models.Model):
     title = models.CharField(max_length=128)

     class Meta:
         ordering = ['title',]

when I do A.objects.all() I want to sort by title. But when 
B.foo_set.all() I would like to always order by A.number.

It seems like a custom manager is the path here, but there does not 
appear to be a way to define one when defining the relationship.

I know I could have a method on Bar like:

def my_foos(self):
     return self.foo_set.all().order_by('number')

But that means the user of the model has to always remember to use the 
method instead of the related name which to me at least does not seem 
much better than always remembering to use order_by().

Ideas?

Is there a reason why custom managers can not be specified for 
relations? It seems like simply passing related_manager=MyCustomManager 
would work and make sense within the existing framework. Quickly looking 
at Trac I see #3871 with an old patch and no real traction. I'd be 
willing to work with the Django team if this is something that would be 
accepted.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to