在 Django 中放置基于性别的默认个人资料图片

Place default profile image based on gender in django

我需要有关 select 根据用户性别设置默认个人资料图片的帮助。 我的媒体文件夹中有三个默认图像,我想将它们用作默认图像,即“00.png、01.png 和 02.png”。

models.py

GenderChoice = (
    ('others', 'Others'),
    ('male', 'Male'),
    ('female' :'Female')
) 

class User(AbstractBaseUser):
    gender = models.CharField(choice=GenderChoice)
    pro_pic = models.ImageField(upload_to ="", default ="00.png")

我想要的是,如果用户 select 的性别="others",那么 00.png 应该保存为默认值,如果他们 select 男性 01.png 应该 select 为默认值..

请帮忙

如果你把它想成 "I want to show a different default based on gender if the user does not have an image uploaded":

就容易多了
from django.templatetags.static import static
class User(AbstractBaseUser):
    gender = models.CharField(choice=GenderChoice)
    pro_pic = models.ImageField(upload_to ="", null=True)
    default_pic_mapping = { 'others': '00.png', 'male': '01.png', 'female': '02.png'}

    def get_profile_pic_url(self):
        if not self.pro_pic:
            return static('img/{}'.format(self.default_pic_mapping[self.gender]))
        return self.pro_pic.url