Question

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.

Was it helpful?

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.

OTHER TIPS

if the order of the images is the order that they are uploaded you could use a timestamp to order them,.

I used the same method (with integer "order" field in the model) to define the ordering. However, I customized the admin to allow drag and drop the images belong to an album to define the order. When the admin hits "save" button, the order will be calculated automatically based on the current order after drag-and-drop. All data will be submitted to the server for saving to DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top