What is wrong with gallery.photos.all().order_by('name') ?
On Feb 27, 4:49 pm, Kyle Fox <[email protected]> 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 [email protected].
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.