Reputation: 110083
I have a user with a profile picture. The picture will be converted into two sizes: one which will be displayed for his profile (approx 200px. wide) and a smaller thumbnail that will be displayed in search results (approx 64 px. wide).
What would be the best way to construct the database and folder structure for this?
My two ideas for db structure are:
# 1)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
...
avatar = models.ImageField(upload_to='images/%Y/%m/%d', blank=True,)
avatar_thumbnail = models.ImageField(upload_to='images/%Y/%m/%d', blank=True,)
# 2)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
...
class Avatar(models.Model):
avatar = models.ImageField(upload_to='images/%Y/%m/%d')
user = models.ForeignKey(UserProfile)
Which one would be a better way to handle this? And what would be a good way to structure my image folder to easily separate and reference the two image sizes? Thank you.
Upvotes: 1
Views: 194
Reputation: 729
I'd argue that the second option would actually be better. Potentially split out into a separate app altogether. I've found it's much better to keep functionality compartmentalized. keep everything related to your avatar in one app and keep the thumbnailing code etc part of that, as well as creating a template tag for displaying the avatar.
there might already be an app for that...
Upvotes: 1
Reputation: 34553
You could use Sorl Thumbnail, upload one image and create the smaller version on the fly using Sorl's template tag(s). This would also save you from having to create another class. I've had a lot of success with this library, hope it helps you out.
http://pypi.python.org/pypi/sorl-thumbnail/11.05.2
Upvotes: 2