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.1.1.tar.gz
.
File metadata
- Download URL: flask-softdelete-1.1.1.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 | 5f2d6186a16989a9df7c3e569ca22e03a98494061037e8531d4afb08069e9d4e |
|
MD5 | de9707eeea06ad2f83bd760b6a602505 |
|
BLAKE2b-256 | e827213ae3a7fa43b49710fb5b921cd6154c6169b5d743c2fa4d2836df1602c4 |
File details
Details for the file flask_softdelete-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: flask_softdelete-1.1.1-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 | cbf989735ff9ad43255d673573c6cfe89833f65fbe6337b22093e33b8bf6b872 |
|
MD5 | 26de28b8035be26b2910eebb3be18934 |
|
BLAKE2b-256 | dd98028e98511f2945d4c3918722ac001bde67797fc9e13be5f17d9806a8de59 |