Search code examples
djangosortingimagegallery

How to force order of pictures in a gallery


I'm building a django app that has an image gallery, and the client insists the images be displayed in specific order. I use the admin interface to upload the images and edit their properties, and I have an ImageFile class in my model that basically looks like this:

class ImageFile(models.Model):
    """represents an image file"""

    # the image description
    description = models.CharField(max_length=45)

    # the actual image
    image = models.ImageFile(upload_to='images') 

    # running number representing the order on the page
    order = models.IntegerField()

    def __unicode__(self):
          return "%s" % (self.description)

    class Meta:
        db_table = 'images'

I'm using the IntegerField 'order' to have running number that'll control the sorting. I figured there has to be a smarter/better way to do this (another model?) and also be able to easily control it through the admin interface.


Solution

  • I suppouse you would like give possibility to sort images to user, (anyway if you want sort it via time add, best way is order it by id), so, if there is model like Gallery (of images) maybe you should store tuple of ids of images from the galery (in DB as a text object). After read cast it to tuple, and you have expected order. Hope I help.