Fix failures due to unsaved tracked instances in RelationCounterField.

Review Request #7157 — Created April 2, 2015 and submitted — Latest diff uploaded

Information

Djblets
release-0.8.x
b4afe22...

Reviewers

Instances of models that were created but never saved caused some
probelms for RelationCounterField. Since we can't listen for post_save
on an instance, we must listen on a class and pass an instance to
compare against. We then tried to disconnect that signal later, but only
if we ever saved that instance.

This meant two bad things were happening:

1. Future saves from other instances of that class would trigger the
   same handler and hit an assertion.
2. We had a reference to that instance, forever, unless it eventually
   saved, and kept its signal handler around forever as well.

That assertion now lives within the conditionals checking if the signal
handler is operating on the expected object, fixing case #1.

For case #2, we no longer keep the instance around in the lambdas for
comparison. Instead, we work on an instance ID. We then keep a weak
reference to the instance in order to track when it falls out of scope,
and disconnect the signal handler.

The end result is that multiple instances from the same class won't
break each other, nor will unsaved instances leak memory.

The new unit tests both failed before this. After, they pass.