Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Theo dõi lịch sử thay đổi đối tượng model trong Django


Gói django-simple-history tự động ghi log mọi thay đổi (tạo, sửa, xóa) của model, cho phép khôi phục dữ liệu đã xóa.

Cách dùng

  1. pip install django-simple-history

  2. Thêm app và middleware:

    INSTALLED_APPS += ['simple_history']
    MIDDLEWARE += ['simple_history.middleware.HistoryRequestMiddleware']
  3. Trong model: thêm trường history = HistoricalRecords()

  4. Admin: đăng ký với SimpleHistoryAdmin.

  5. Chạy migration, tạo superuser, vào admin xem tab "History".

Tính năng

  • Xem diff thay đổi từng field.

  • Khôi phục bản ghi đã xóa.

  • Truy vấn lịch sử qua shell hoặc view.