I am designing a website using django. The idea of a website is to have photographers and each photographer would have an album or more albums and each album would have one or more photo/photos. Currently I have:
class Photo(models.Model):
name=models.ForeignKey('Album')
p=models.ImageField()
def __unicode__(self):
return self.name
class Album(models.Model):
name=models.ForeignKey('Photographer')
def __unicode__(self):
return self.name
class Photographer(models.Model):
name=models.ForeignKey('User')
def __unicode__(self):
return self.name
The question is if you have the same design would you create a directory for each photographer and for each album you would create another directory in the parent directory and then store the photos in that album's directory or store them in the database. I know that is a newbie question but I really need help. If you have other design to make it easier on me please let me know.
Thanks in advance, Abdul
-
Uh... I'd let the storage handler deal with it for me...Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2012年12月01日 02:31:45 +00:00Commented Dec 1, 2012 at 2:31
3 Answers 3
Use the imagefield to store all the photos at one place and even better store them in cloud storage services. And as to regarding the relation have ForeignKeys in Photo model relating to Album and Photographer. Than use reverse relationship to find out the photos of each album or Photographer.
Your code might look like this`
class Photo(models.Model):
image = ImageField(upload_to="whereever you want to")
album = ForeignKey(Album)
photographer=ForeignKey(Photographer)
and after you get a particular album you can use Album.set_all and photographer.set_all to get all the photos of a particular album or photographer
`
Comments
Yes, store the photos directly in the file system, rather than database. This is a common practise which makes sense for many reasons, mostly related to performance.
If you expect to have thousends of photos then dont keep them one directory, cause that can cause performance issues on popular systems. Instead, design a simple hierarchy appropriate for your application.
3 Comments
non Latin later and conflate? Since it stores the files on disk, then it can store all files with valid filenames, which includes some non-latin characters.There is no need to create directories for photographer or an album. That is what databases are for. You can have one giant folder (specified in upload_to) which will contain all uploaded images for all photographers and albums and then its up for a db to keep track which photos belong to what photographer/album.
By the way you should specify upload_to parameter for ImageField:
models.ImageField(upload_to='images/')
If you really want to create folders for each album, you can pass a function to upload_to parameter and add whatever logic you want there... more about that in the docs here.