Just to be clear, the ManyToManyField will have 'a', 'c', 'b' listed under Person1's admin interface with 'a' and 'c' highlighted. What I want to achieve is to order the name => 'a', 'b', 'c' with 'a', 'c' highlighted for Person1 and 'b' highlighted for Person2.
-Aaron On Wed, Jan 28, 2009 at 6:02 PM, Aaron Lee <waifun...@gmail.com> wrote: > > > On Wed, Jan 28, 2009 at 4:44 PM, Malcolm Tredinnick < > malc...@pointy-stick.com> wrote: > >> >> On Wed, 2009-01-28 at 06:49 -0800, Aaron Lee wrote: >> > I tried doing that but it didn't work, the order of names within the >> > PersonAdmin still shows the Name according to pk order. >> >> Firstly, realise that your question doesn't have a "correct" answer. >> Ordering by multi-valued fields always have that problem. For example: >> >> Person1 >> - Name.name = 'a' >> - Name.name = 'c' >> Person2 >> - Name.name = 'b' >> > > What I want to achieve is when I edit Person1 in the admin interface, I > want to order the Name.name i.e. 'a', 'c' > instead of according to 'a','c''s primary key in Name. So I am a bit > confused why Person2 comes into the picture. > Think about a person can have multiple names e.g. screenname, nickname, > username etc ... > > I believe that's a legitimate use of ManyToManyField? > > >> >> Now, depending upon which "name" value is chosen for Person1, it might >> sort ahead of or behind Person2. However, things are even worse than >> that. What will happen, in fact, is that the SQL results will include >> Person1 twice -- once for name='a' and once for name='c', since any >> ordering columns are part of the select result. >> >> However, if you're certain that the group of things you are selecting >> don't have multiple entries in the related many-to-many table (in which >> case, why are you using a ManyToManyField and not a ForeignKey?), you >> can order by any related field you like: >> >> class Person(models.Model): >> ... >> class Meta: >> ordering = ['name__name'] >> >> This is documented in the order_by() documentation for querysets (since >> Meta.ordering is just an order_by() application): >> >> http://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by-fields >> >> Regards, >> Malcolm >> >> >> >> >> >> > --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---