Skip to content

Commit

Permalink
fix: Avoid error getting translated content for unsaved or deleted bl…
Browse files Browse the repository at this point in the history
…ocks in __str__ method
  • Loading branch information
drikusroor committed Jan 7, 2025
1 parent 591d93c commit 61801b7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions backend/experiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,15 @@ class Block(models.Model):
theme_config = models.ForeignKey(ThemeConfig, on_delete=models.SET_NULL, blank=True, null=True)

def __str__(self):
content = self.get_fallback_content()
return content.name if content and content.name else self.slug
# If this block is unsaved or being deleted (has no PK),
# avoid calling get_fallback_content() (which does a DB query).
if not self.pk:
# Provide a fallback label or just return self.slug if present.
return self.slug or "Deleted Block"

# change message
def save(self, *args, **kwargs):
super().save(*args, **kwargs)

@property
def name(self):
Expand Down Expand Up @@ -450,10 +457,6 @@ def get_fallback_content(self) -> "BlockTranslatedContent | None":
Fallback content
"""

# If the block is not saved yet or is being deleted, there is no fallback content
if not self.pk:
return None

if not self.phase or self.phase.experiment:
return self.translated_contents.first()

Expand Down

0 comments on commit 61801b7

Please sign in to comment.