|
1 |
| - |
2 | 1 | # django-handleref
|
3 | 2 |
|
4 | 3 | [](https://pypi.python.org/pypi/django-handleref)
|
5 | 4 | [](https://pypi.python.org/pypi/django-handleref)
|
6 | 5 | [](https://github.com/20c/django-handleref)
|
7 | 6 | [](https://codecov.io/github/20c/django-handleref)
|
8 | 7 |
|
9 |
| -track when an object was created or changed and allow querying based on time and versioning (w/ django-reversion support) |
10 |
| - |
11 |
| -## HandleRefModel as a base for all your models |
12 |
| - |
13 |
| - from django.db import models |
14 |
| - from django_handleref.models import HandleRefModel |
15 |
| - |
16 |
| - class Test(HandleRefModel): |
17 |
| - name = models.CharField(max_length=255) |
18 |
| - |
19 |
| -## Querying for modification since |
20 |
| - |
21 |
| -It is now possible for you to see which instances of a model have been created or modified |
22 |
| -since a specific time or version |
23 |
| - |
24 |
| - import time |
25 |
| - |
26 |
| - # create instance |
27 |
| - test = Test.objects.create(name="This is a test") |
28 |
| - |
29 |
| - # keep track of time, we need this later |
30 |
| - t = time.time() |
31 |
| - |
32 |
| - # modifying and saving it |
33 |
| - test.name = "Changed my mind" |
34 |
| - test.save() |
35 |
| - |
36 |
| - # now we can use the handleref manager to retrieve it |
37 |
| - Test.handleref.since(timestamp=t).count() # 1 |
38 |
| - Test.handleref.since(timestamp=time.time().count() #0 |
39 |
| - |
40 |
| - |
41 |
| -## Soft delete |
42 |
| - |
43 |
| -By default all models extending HandleRefModel will softdelete when their delete() method is called. |
44 |
| -Note that this currently wont work for batch deletes - as this does not call the delete() method. |
45 |
| - |
46 |
| - test = Test.objects.get(id=1) |
47 |
| - test.delete() |
48 |
| - |
49 |
| - # querying handleref undeleted will not contain the deleted object |
50 |
| - Test.handleref.undeleted().filter(id=1).count() #0 |
51 |
| - |
52 |
| - # querying handleref since will not contain the deleted object |
53 |
| - Test.handleref.since(timestamp=t).filter(id=1).count() #0 |
54 |
| - |
55 |
| - # passing deleted=True to handleref since will contain the deleted object |
56 |
| - Test.handleref.since(timestamp=t, deleted=True).filter(id=1).count() #1 |
57 |
| - |
58 |
| - # querying using the standard objects manager will contain the deleted object |
59 |
| - Test.objects.filter(id=1).count() #1 |
60 |
| - |
61 |
| - # You may also still hard-delete by passing hard=True to delete |
62 |
| - test.delete(hard=True) |
63 |
| - Test.objects.filter(id=1).count() #0 |
64 |
| - |
65 |
| -## Versioning (with django-reversion) |
66 |
| - |
67 |
| -Requires |
68 |
| - |
69 |
| - django-reversion==1.8.7 |
70 |
| - |
71 |
| -When django-reversion is installed all your HandleRefModels will be valid for versioning. |
72 |
| - |
73 |
| - import reversion |
74 |
| - |
75 |
| - with reversion.create_revision(): |
76 |
| - obj = Test.objects.create(name="This is a test") |
77 |
| - obj.save() |
78 |
| - |
79 |
| - obj.version #1 |
80 |
| - |
81 |
| - obj.name = "Changed my mind" |
82 |
| - obj.save() |
83 |
| - |
84 |
| - obj.version #2 |
85 |
| - |
86 |
| - Test.handleref.since(version=1).count() #1 |
87 |
| - |
| 8 | +Track when an object was created or changed and allow querying based on time and versioning (w/ `django-reversion` support) |
88 | 9 |
|
89 | 10 | ### License
|
90 | 11 |
|
91 | 12 | Copyright 2015-2022 20C, LLC
|
92 | 13 |
|
93 | 14 | Licensed under the Apache License, Version 2.0 (the "License");
|
94 |
| -you may not use this softare except in compliance with the License. |
| 15 | +you may not use this software except in compliance with the License. |
95 | 16 | You may obtain a copy of the License at
|
96 | 17 |
|
97 | 18 | http://www.apache.org/licenses/LICENSE-2.0
|
|
0 commit comments