I have this create / update function in my serializer:

    def create(self, validated_data):
    user = models.User(
        organization_code=validated_data['organization_code'],
        username=validated_data['username'],
        personal_code=validated_data['personal_code'],
        created_by=validated_data['created_by'],
        role=validated_data['role'],
        description=self.initial_data['description'],
        start_at=self.initial_data['start_at'],
        end_at=self.initial_data['end_at']
    )
    user.set_password(validated_data['password'])
    user.save()
    return user

    def update(self, instance, validated_data):
    instance.username = validated_data.get('username', instance.username)
    instance.personal_code = validated_data.get('personal_code', instance.personal_code)
    instance.organization_code = validated_data.get('personal_code', instance.organization_code)
    instance.role = validated_data.get('role', instance.role)
    instance.published = validated_data.get('published', instance.published)
    instance.updated_by = validated_data.get('updated_by', instance.updated_by)
    instance.updated_at = validated_data.get('updated_at', instance.updated_at)
    instance.description = validated_data.get('description', instance.description)
    instance.start_at = validated_data.get('start_at', instance.start_at)
    instance.end_at = validated_data.get('end_at', instance.end_at)
    instance.is_notified_seen = validated_data.get('is_notified_seen', 0)
    instance.save()
    return instance

my front end do not send key if a field is not filed by user(for example some json from front end has no key of 'description') now I check in my view and if key doeas not exist I add key to querydict and then I send it to serializer, but I dont want to do it any more and I want in my create function check if key exist then save it and if not do nothing, is there a way about this?

0 Answers