Skip to main content

django object tracking

Project description

django-handleref

PyPI PyPI Tests LGTM Grade Codecov

track when an object was created or changed and allow querying based on time and versioning (w/ django-reversion support)

HandleRefModel as a base for all your models

from django.db import models
from django_handleref.models import HandleRefModel

class Test(HandleRefModel):
    name = models.CharField(max_length=255)

Querying for modification since

It is now possible for you to see which instances of a model have been created or modified since a specific time or version

import time

# create instance
test = Test.objects.create(name="This is a test")

# keep track of time, we need this later
t = time.time()

# modifying and saving it
test.name = "Changed my mind"
test.save()

# now we can use the handleref manager to retrieve it
Test.handleref.since(timestamp=t).count() # 1
Test.handleref.since(timestamp=time.time().count() #0

Soft delete

By default all models extending HandleRefModel will softdelete when their delete() method is called. Note that this currently wont work for batch deletes - as this does not call the delete() method.

test = Test.objects.get(id=1)
test.delete()

# querying handleref undeleted will not contain the deleted object
Test.handleref.undeleted().filter(id=1).count() #0

# querying handleref since will not contain the deleted object
Test.handleref.since(timestamp=t).filter(id=1).count() #0

# passing deleted=True to handleref since will contain the deleted object
Test.handleref.since(timestamp=t, deleted=True).filter(id=1).count() #1

# querying using the standard objects manager will contain the deleted object
Test.objects.filter(id=1).count() #1

# You may also still hard-delete by passing hard=True to delete
test.delete(hard=True)
Test.objects.filter(id=1).count() #0

Versioning (with django-reversion)

Requires

django-reversion==1.8.7

When django-reversion is installed all your HandleRefModels will be valid for versioning.

import reversion

with reversion.create_revision():
    obj = Test.objects.create(name="This is a test")
    obj.save()

    obj.version #1

    obj.name = "Changed my mind"
    obj.save()

    obj.version #2

Test.handleref.since(version=1).count() #1

License

Copyright 2015-2021 20C, LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this softare except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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

django-handleref-1.0.1.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

django_handleref-1.0.1-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file django-handleref-1.0.1.tar.gz.

File metadata

  • Download URL: django-handleref-1.0.1.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.0

File hashes

Hashes for django-handleref-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f822d98a896cfae6a8d3e70448b68e6ad0da0a2a6536d0d37b61342789d25054
MD5 19682bbeb5e73f0c3704f2d698516e59
BLAKE2b-256 4fe30b41e6d39ad8eda1340efc5529a993a73eb3f2ca8a98b1451089a87f4f04

See more details on using hashes here.

File details

Details for the file django_handleref-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_handleref-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 27.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.0

File hashes

Hashes for django_handleref-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc787708e57feed668822ca5ae12529db1eb83d41fa3299c8c1e3ea9a2fdb47e
MD5 36837d7c832300b1071bcda2503a0137
BLAKE2b-256 8a0c77a23b70228722f5c95ec64e18791bb99c10f48c926f3ddaa329b4a21493

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page