A simple mixin for adding soft delete functionality to Flask-SQLAlchemy models
Project description
Flask-Softdelete
Flask-SoftDelete is a simple extension for Flask applications that adds soft delete functionality to Flask-SQLAlchemy models. Instead of permanently deleting records, soft deleting allows you to mark records as "deleted" without actually removing them from the database. This is useful for keeping a history of deleted records or allowing for easy restoration.
Table of Contents
Installation
To install Flask-Softdelete, use pip:
pip install Flask-Softdelete
Configuration
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_softdelete import SoftDeleteMixin
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///your_database.db'
db = SQLAlchemy(app)
Usage
Base Model
class SampleModel(db.Model, SoftDeleteMixin):
__tablename__ = 'sample_model'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
Record Management Methods
Flask-Softdelete provides methods for managing soft delete functionality:
soft_delete(): Marks the record as deleted, but keeps it in the database. restore(): Restores a soft-deleted record, making it active again. force_delete(): Permanently removes the record from the database, which cannot be undone.
Examples
Create a new record
sample = SampleModel(name="Example")
db.session.add(sample)
db.session.commit()
# Soft delete the record
sample.soft_delete()
# Restore the record
sample.restore()
# Permanently delete the record
sample.force_delete()
Logging
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Log soft delete action
logger.info(f"Soft deleted record with ID: {sample.id}")
Contributing
If you would like to contribute to Flask-Softdelete, please fork the repository and submit a pull request. You can find the repository on GitHub.
Reporting Issues
If you encounter any issues, please report them in the Issues section of the GitHub repository. This helps improve the package and assists others who might face similar issues.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-softdelete-1.2.0.tar.gz
.
File metadata
- Download URL: flask-softdelete-1.2.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50c67eb62f283a661bdce0fcd8d58688c10d514e4e31229f285189e68755f23a |
|
MD5 | 705c25b43dbe64d3c5eea687e7ee6f9d |
|
BLAKE2b-256 | 6cc68e6a1ae93fcd1d42827b994345596890e482fc9e799e6fc0674e42c3dec6 |
File details
Details for the file flask_softdelete-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: flask_softdelete-1.2.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0984fa32f7a972b90f6d80b5ffe54155d9b962c86218df20be4b6f32165bf0e |
|
MD5 | 16a44d2dd6858afd386d1aa754d2f092 |
|
BLAKE2b-256 | 00991bee2f118fea2e41bcd88b7cf087d9801b8b55301328b6aa0b3e21c7bcfc |