def update_entry_count sender instance raw try old Entry objects get i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def update_entry_count(sender, instance, raw):
try:
old = Entry.objects.get(id=instance._get_pk_val())
except Entry.DoesNotExists:
# new object is going to be created
old = instance
old_tags = set(old.tags.all())
new_tags = set(instance.tags.all())
print 'old', old_tags
print 'new', new_tags
for removed in old_tags - new_tags:
print removed
removed.entry_count = removed.entry_set.count()
removed.total_count = removed.entry_set + removed.release_count
if removed.total_count:
removed.save()
else:
removed.delete()
for tag in new_tags:
tag.entry_count =tag.entry_set.count()
tag.total_count = tag.entry_count + tag.release_count
tag.save()