I don't know if you can do it directly using the ordering metadata - easiest way to find out is to try it and see.
If not, I'd suggest a customer manager on the Gallery that returns the photos in order so that the line: Gallery.photos.all() refers to your customer manager that "does the right thing". Custom managers are really easy and very useful - see http://docs.djangoproject.com/en/dev/topics/db/managers/ Malcolm On Sun, Feb 28, 2010 at 5:07 PM, Kyle Fox <kyle....@gmail.com> wrote: > The user needs to be able to manually position the photos in the > gallery (using drag and drop, for example). A photo can exist in > multiple galleries, which is why the join model GalleryPhoto is > required (and this is where the position gets stored). > > On Feb 27, 7:36 pm, Prabhu <prabhu.subraman...@gmail.com> wrote: > > What is wrong with gallery.photos.all().order_by('name') ? > > > > On Feb 27, 4:49 pm, Kyle Fox <kyle....@gmail.com> wrote: > > > > > > > > > I'm wondering if it's possible to apply ordering to a ManyToMany > > > relationship by using a `position` attribute on the join model. A > > > classic example (photo gallery) is probably the best way to illustrate > > > this: > > > > > class Photo(models.Model): > > > image = models.ImageField(upload_to="photos") > > > > > class Gallery(models.Model): > > > name = models.CharField(max_length=100) > > > photos = models.ManyToManyField(Photo, through='GalleryPhoto') > > > > > class GalleryPhoto(models.Model): > > > gallery = models.ForeignKey(Gallery) > > > photo = models.ForeignKey(Photo) > > > position = models.IntegerField(default=0) > > > > > class Meta: > > > ordering = ('position',) > > > > > (Also athttp://dpaste.com/hold/165618/) > > > > > I want to attach photos with a gallery, like this: > > > > > >>> GalleryPhoto.objects.create(photo=photo1, gallery=some_gallery, > position=1) > > > >>> GalleryPhoto.objects.create(photo=photo2, gallery=some_gallery, > position=2) > > > > > And then have the photos retrievable through the gallery *according to > > > the position attribute* on the GalleryPhoto, like so: > > > > > >>> gallery.photos.all() > > > > > [photo1, photo2] > > > > > The simplest fix would be to add create a `get_photos` method on the > > > Gallery which does a query for it's photos, but I'd rather stick to > > > straight Django models if at all possible. > > > > > Thanks! > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.