From 276141950cfc8b899e0b1446b2f581a611f6dcbc Mon Sep 17 00:00:00 2001 From: Paul Traylor Date: Wed, 17 Apr 2024 16:31:39 +0900 Subject: [PATCH] Fix for foreign key check regarding m2m Some of the behavior changed between Django 3.2 and 4.2 so we make a fix here to get around it. Also add the rule pk to the __str__ for us to use while debugging --- promgen/forms.py | 8 ++++++++ promgen/models.py | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/promgen/forms.py b/promgen/forms.py index 2479debd3..857213431 100644 --- a/promgen/forms.py +++ b/promgen/forms.py @@ -153,6 +153,14 @@ def clean(self): super().clean() rule = models.Rule(**self.cleaned_data) + # In Django https://code.djangoproject.com/ticket/19580, some of the + # foreign key checks got stricter. We sets pk to 0 here so that it passes + # django's m2m/foreign key checks, but marks for us that it's a temporary + # rule that doesn't actually exist. + # We'll likely want to rework this assumption when we move to a different + # promql check + rule.pk = 0 + # Make sure we pull in our labels and annotations for # testing if needed # See django docs on cached_property diff --git a/promgen/models.py b/promgen/models.py index 77014d772..03bd89cc0 100644 --- a/promgen/models.py +++ b/promgen/models.py @@ -423,9 +423,9 @@ class Meta: def __str__(self): return ( - f"{self.name}" + f"{self.pk}:{self.name}" if self.content_object is None - else f"{self.name} [{self.content_object.name}]" + else f"{self.pk}:{self.name} [{self.content_object.name}]" ) def get_absolute_url(self):