import random
from django.db import models
from django.conf import settings
class Profile(models.Model):
@staticmethod
def _get_random_photo():
photos_dir = (
settings.STATIC_ROOT / 'images' / 'default_profile_photos'
)
photo_paths = photos_dir.rglob('avatar*.png')
return random.choice(photo_paths)
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
date_of_birth = models.DateField(blank=True, null=True)
photo = models.ImageField(
upload_to='users/%Y/%m/%d',
blank=True,
default=_get_random_photo,
)
def __str__(self):
return f'Profile for user {self.user.username}'