These are chat archives for django/django

9th
Jul 2015
Jonathan Lin
@jonzlin95
Jul 09 2015 10:08
This message was deleted
Jonathan Lin
@jonzlin95
Jul 09 2015 10:14

Hi all, I'm writing tests for an F() expression and noticed a quirk on save

@receiver(post_save, sender=Fan)
def fan_create_callback(sender, instance=None, created=False, **kwargs):
    if created:
        f = instance.followable
        f.follower_count = F('follower_count') + 1
        f.save()

and the corresponding test code

    def test_fan_creation(self):
        f = PlayerFan(user=self.user, followable=self.fol)
        f.save()
        self.assertEqual(self.fol.follower_count, 1)

but it fails with

Traceback (most recent call last):
  File "/Users/jonathanlin/PyCharmProjects/ies_user/ies_user/apps/followable/tests.py", line 22, in test_fan_creation
    self.assertEqual(self.fol.follower_count, 1)
AssertionError: <CombinedExpression: F(follower_count) + Value(1)> != 1

however if I rewrite the receiver as

@receiver(post_save, sender=Fan)
def fan_create_callback(sender, instance=None, created=False, **kwargs):
    if created:
        f = Followable.objects.get(pk=instance.followable.pk)
        f.follower_count = F('follower_count') + 1
        f.save()

The same test works. Just wondering if this is expected behavior, and if so I'd love to understand why.
Thanks in advance!