Skip to content

Commit

Permalink
13599 fix cached counter for edit object (#13600)
Browse files Browse the repository at this point in the history
* 13599 fix cache counter

* 13599 update test

* Merge conditionals

---------

Co-authored-by: Jeremy Stretch <[email protected]>
  • Loading branch information
arthanson and jeremystretch authored Aug 29, 2023
1 parent 83536fb commit 065a40d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions netbox/utilities/counters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def update_counter(model, pk, counter_name, value):
# Signal handlers
#

def post_save_receiver(sender, instance, **kwargs):
def post_save_receiver(sender, instance, created, **kwargs):
"""
Update counter fields on related objects when a TrackingModelMixin subclass is created or modified.
"""
Expand All @@ -39,7 +39,7 @@ def post_save_receiver(sender, instance, **kwargs):
# Update the counters on the old and/or new parents as needed
if old_pk is not None:
update_counter(parent_model, old_pk, counter_name, -1)
if new_pk is not None:
if new_pk is not None and (old_pk or created):
update_counter(parent_model, new_pk, counter_name, 1)


Expand Down
6 changes: 5 additions & 1 deletion netbox/utilities/tests/test_counters.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ def test_interface_count_creation(self):
self.assertEqual(device1.interface_count, 2)
self.assertEqual(device2.interface_count, 2)

Interface.objects.create(device=device1, name='Interface 5')
interface1 = Interface.objects.create(device=device1, name='Interface 5')
Interface.objects.create(device=device2, name='Interface 6')
device1.refresh_from_db()
device2.refresh_from_db()
self.assertEqual(device1.interface_count, 3)
self.assertEqual(device2.interface_count, 3)

interface1.save()
device1.refresh_from_db()
self.assertEqual(device1.interface_count, 3)

def test_interface_count_deletion(self):
"""
When a tracked object (Interface) is deleted the tracking counter should be updated.
Expand Down

0 comments on commit 065a40d

Please sign in to comment.