|
3 | 3 |
|
4 | 4 | from django.contrib.contenttypes.models import ContentType |
5 | 5 | from django.contrib.postgres.fields import ArrayField |
| 6 | +from django.core.exceptions import ObjectDoesNotExist |
6 | 7 | from django.core.validators import MaxValueValidator, MinValueValidator |
7 | 8 | from django.db import models |
8 | 9 | from django.utils.functional import cached_property |
@@ -809,10 +810,15 @@ def save(self, *args, **kwargs): |
809 | 810 | from baserow.contrib.database.formula.ast.function_defs import BaserowCount |
810 | 811 | from baserow.contrib.database.formula.ast.tree import BaserowFieldReference |
811 | 812 |
|
812 | | - field_reference = BaserowFieldReference( |
813 | | - getattr(self.through_field, "name", ""), None, None |
814 | | - ) |
815 | | - self.formula = f"{BaserowCount.type}({field_reference})" |
| 813 | + try: |
| 814 | + through_field = getattr(self, "through_field") |
| 815 | + field_name = getattr(through_field, "name", "") |
| 816 | + except ObjectDoesNotExist: |
| 817 | + field_name = "'invalid through'" |
| 818 | + |
| 819 | + field_ref = BaserowFieldReference(field_name, None, None) |
| 820 | + self.formula = f"{BaserowCount.type}({field_ref})" |
| 821 | + |
816 | 822 | super().save(*args, **kwargs) |
817 | 823 |
|
818 | 824 | def __str__(self): |
@@ -852,13 +858,23 @@ def save(self, *args, **kwargs): |
852 | 858 | formula_function_registry, |
853 | 859 | ) |
854 | 860 |
|
| 861 | + try: |
| 862 | + through_field = getattr(self, "through_field") |
| 863 | + through_name = getattr(through_field, "name", "") |
| 864 | + except ObjectDoesNotExist: |
| 865 | + self.through_field = None |
| 866 | + through_name = "'invalid through'" |
| 867 | + |
| 868 | + try: |
| 869 | + target_field = getattr(self, "target_field") |
| 870 | + target_name = getattr(target_field, "name", "") |
| 871 | + except ObjectDoesNotExist: |
| 872 | + self.target_field = None |
| 873 | + target_name = "'invalid target'" |
| 874 | + |
855 | 875 | formula_function = formula_function_registry.get(self.rollup_function) |
856 | | - field_reference = BaserowFieldReference( |
857 | | - getattr(self.through_field, "name", ""), |
858 | | - getattr(self.target_field, "name", ""), |
859 | | - None, |
860 | | - ) |
861 | | - self.formula = f"{formula_function.type}({field_reference})" |
| 876 | + field_ref = BaserowFieldReference(through_name, target_name, None) |
| 877 | + self.formula = f"{formula_function.type}({field_ref})" |
862 | 878 | super().save(*args, **kwargs) |
863 | 879 |
|
864 | 880 | def __str__(self): |
|
0 commit comments