Home > Blockchain >  How do I set default image based on gender in django-rest-framework?
How do I set default image based on gender in django-rest-framework?

Time:11-15

I know there are several similar questions here, but none of them seem to resolve my issue. I am using Django-Rest-Framework.

I am creating the user-profile simultaneously with the creation of user using signals.

As my question is pretty self explanatory, this is my code

models.py

from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.conf import settings
from PIL import Image

GENDER_SELECTION = (
    ('Male', 'Male'),
    ('Female', 'Female'),
)

class CustomUser(AbstractUser):
    username = models.CharField(max_length=100, blank=True, null=True)
    email = models.EmailField(_('email address'), unique=True)
    gender = models.CharField(max_length=20, choices=GENDER_SELECTION)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name', 'gender']

    def __str__(self):
        return self.email


class UserProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profile')
    profile_pic = models.ImageField(upload_to='profile/', default='default.png', blank=True)

    def __str__(self):
        return f'{self.user.first_name} Profile'

    def save(self, *args, **kwargs):
        super(UserProfile, self).save(*args, **kwargs)

        uploaded_image = Image.open(self.profile_pic.path)

        if uploaded_image.height > 300 or uploaded_image.width > 300:
            output_size = (300, 300)
            uploaded_image.thumbnail(output_size)
            uploaded_image.save(self.profile_pic.path)

This is what I have tried

In models.py

# ...

def save(self, *args, **kwargs):
    super(Profile, self).save(*args, **kwargs)

    if self.profile_pic == 'default.png':
        if CustomUser.gender == 'Male':
            self.profile_pic = 'user_default_m.png'
            return self.profile_pic
        else:
            self.profile_pic = 'user_default_f.png'
            return self.profile_pic
    else:
        uploaded_image = Image.open(self.profile_pic.path)
        if uploaded_image.height > 300 or uploaded_image.width > 300:
            output_size = (300, 300)
            uploaded_image.thumbnail(output_size)
            uploaded_image.save(self.profile_pic.path)

Update - Pasting signals.py file as requested

signals.py

@receiver(post_save, sender=CustomUser)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=CustomUser)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

CodePudding user response:

Here is something I noticed, you are trying to resize the uploaded image and you are trying to set a default-image in the same go. Also you are trying to do this post_save.

I am assuming you are allowing upload of image by the user when he edits his/her profile which I am inferring since you have over-ridden the save method.

I would try to avoid clubbing those two. Even though they are related to Image operations, in-essence, they are different operations.

Keep your models.py file lean and restore it to your initial code.

Now, as you are already using signals, you can try pre_save in your signals.py file. Edit your signals.py file and add the following code snippet

@receiver(pre_save, sender=UserProfile)
def set_profile_image(sender, instance, *args, **kwargs):

    # Obtain the gender of the just created user
    gender = CustomUser.objects.all().last().gender

    # Check for the gender and assign the file
    if gender == 'Male' and instance.profile_pic == 'default.png':
        instance.profile_pic = 'default_m.png'
    elif gender == 'Female' and instance.profile_pic == 'default.png':
        instance.profile_pic = 'default_f.png'

This should work.

  • Related