views:

63

answers:

2

Hi - 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. Any ideas will be great - thanks.

+1  A: 

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.

Rin
This is probably the best idea, except that you might be better storing the ordering information in a serialized data format such as JSON or Pickle.
jathanism
A: 

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

George