How to set a custom manager class parameter to required=False

156 views Asked by At

I am writing an API to accept and save the user profile information. In total I want to accept 5 parameters from the user which are mentioned below.

{
"first_name":"",
"last_name":"",
"email":"",
"password":"",
"profile_picture":""
}

Out of these 5 parameters first_name, last_name, email and password are mandatory and profile_picture is optional.

In models.py I have mentioned profile_picture as an ImageField.

models.py

class Users(AbstractBaseUser, PermissionsMixin):

    """
    This model is used to store user login credential and profile information.
    It's a custome user model but used for Django's default authentication.
    """

    email = models.EmailField(max_length=255, unique=True)
    first_name = models.CharField(max_length=255, blank=False, null=False)
    last_name = models.CharField(max_length=255, blank=False, null=False)
    profile_picture = models.ImageField(upload_to='profile_picture/', max_length=None, blank=True)
    is_active = models.BooleanField(default=True)

    # defining a custom user manager class for the custom user model.
    objects = managers.UserManager()

    # using email a unique identity for the user and it will also allow user to use email while logging in.
    USERNAME_FIELD = 'email'

In serializers.py I have mentioned profile_picture as an ImageField with required=False.

serializers.py

class UserSerializer(serializers.Serializer):
    """
    This is a serializer class to validate create user request object.
    """
    class Meta:
        models = 'Users'

    email = serializers.EmailField(allow_blank=False)
    first_name = serializers.CharField(max_length=255, allow_blank=False)
    last_name = serializers.CharField(max_length=255, allow_blank=False)
    profile_picture = serializers.ImageField(max_length=None, allow_empty_file=True, use_url= False, required=False)
    password = serializers.CharField(
        max_length = 255,
        write_only=True,
        allow_blank = False,
        style={
            'input_type' : 'password'
        }
    )

    def create(self, validated_data):

        user_id = models.Users.objects.create_user(**validated_data)

        return user_id

And this is how my custom manager class looks like.

manager.py

class UserManager(BaseUserManager):
    """
    Manager class for the custome user manager. Every model has a default manager class.
    If you define a custome user model then model manager class also needs some customization. 
    """

    def create_user(self, email, first_name, profile_picture, last_name, password=None):
        """
        Here the default create_user() methode of the manager is overridden to achive customization.
        """

        # check email parameter is has some value ir its empty.
        if not email:

            # raise the value error if it is empty.
            raise ValueError('Users must have an email address.')

        # defining a model instance by mapping parameters with the model fields.
        user = self.model(
            # normalizing email i.e converting it to lower case.
            email = self.normalize_email(email),
            first_name = first_name,
            last_name = last_name,
            profile_picture = profile_picture,
            )
        # encrypting user password using default method set_password() 
        user.set_password(password)

        # saving instance in to model.
        user.save(using=self._db)

        # return model instance.
        return user

When I pass the object without profile_picture it returns me the below mentioned error.

TypeError at /user/create/
create_user() missing 1 required positional argument: 'profile_picture'

Hence I tried once again after removing profile_picture parameter from create_user() method of manager class. But then the API fails for the users who wants to upload their profile_picture.

3

There are 3 answers

0
danish_wani On BEST ANSWER

First things first: You need to keep null=True for the image field as blank is for frontend validation and null is for backend validation.

profile_picture = models.ImageField(upload_to='profile_picture/', max_length=None, blank=True, null=True)

Secondly, set the profile_picture as keyword argument if you want to make it optional.

def create_user(self, email, first_name, last_name, password=None,profile_picture=None):
0
Lothric On

To set some field to be not required you should add null=True and blank=True to it, so try to add null=True in your models.py:

profile_picture = models.ImageField(..., null=True)
0
Newton Karanu On

You can set a default value for the profile photo until when the user changes it

profile_picture = models.ImageField(..., null=True, default="path/to/default/image.jpeg")