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