Сигналы django pre_save не обновляются
Последняя строка теста внизу не работает. Я попытался сократить код до самых необходимых частей. Объяснение находится в комментариях к коду.
class Concept(model.Model):
is_deprecated= Boolean(default=False)
...
class Tag(model.Model):
concept_tags = ManyToManyField(Concepts, ...)
is_deprecated= Boolean(default=False)
...
class StatusInfo(model.Model):
"""the important thing is the pre_save signal"""
status = ''
affected_tag = ForeignKey(Tag, ...)
...
@receiver(pre_save, sender=StatusInfo)
def status_change_modification(sender, instance, *args, **kwargs):
if instance.status == "d": # d for deprecated
instance.affected_tag.is_deprecated = True # works
c_tags = instance.affected_tag.concept_tags.all()
print(f"{c_tags = }") # when I run the test below this shows that I have included self.concept_with_deprecated_tag
c_tags.update(is_deprecated=True) # doesn't seem to work but I don't want to trigger a save() on concepts (referenced through concept_tags) because concepts has it's own signals that will trigger.
##### in the tests/status_info.py #######
def test_status_set_tag_info_deprecated(self):
"""
if you add the statusInfo to a tag it will set the tag
and related concepts to is_deprecated == True
"""
self.concept_with_deprecated_tag.is_deprecated = False # set temporarily for the test
self.assertFalse(self.deprecated_tag.is_deprecated)
self.main_status_info = StatusInfoFactory(
affected_tag=self.deprecated_tag,
status="d",
by=self.new_replacement_tag)
self.assertTrue(self.deprecated_tag.is_deprecated)
self.assertTrue(self.concept_with_deprecated_tag.is_deprecated) # this is one concept from the list and it fails to be changed.