Skip to content

Commit a53cfe2

Browse files
committed
Handle programming error when the apps or the db are not ready
1 parent 98ee943 commit a53cfe2

File tree

1 file changed

+30
-24
lines changed
  • django_features/custom_fields/models

1 file changed

+30
-24
lines changed

django_features/custom_fields/models/base.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.contrib.contenttypes.fields import GenericRelation
44
from django.contrib.postgres.expressions import ArraySubquery
55
from django.db import models
6+
from django.db import ProgrammingError
67
from django.db.models import OuterRef
78
from django.db.models import Q
89
from django.db.models import QuerySet
@@ -76,34 +77,39 @@ def get_queryset(self) -> QuerySet:
7677
"""
7778
We filter all available custom fields for the current model.
7879
"""
79-
available_fields = CustomField.objects.for_model(self.model)
80-
81-
"""
82-
This for loop creates a dict with all available custom field values with a subquery for the specific object.
83-
The dict key is the identifier of the custom field amd the value is the custom value.
84-
If the object has no value for the field, it will return None.
85-
More information can be found in the django documentation:
86-
https://docs.djangoproject.com/en/5.2/ref/models/expressions/#subquery-expressions
87-
"""
88-
fields = {field.identifier: self._subquery(field) for field in available_fields}
80+
try:
81+
available_fields = CustomField.objects.for_model(self.model)
82+
83+
"""
84+
This for loop creates a dict with all available custom field values with a subquery for the specific object.
85+
The dict key is the identifier of the custom field amd the value is the custom value.
86+
If the object has no value for the field, it will return None.
87+
More information can be found in the django documentation:
88+
https://docs.djangoproject.com/en/5.2/ref/models/expressions/#subquery-expressions
89+
"""
90+
fields = {
91+
field.identifier: self._subquery(field) for field in available_fields
92+
}
8993

90-
"""
91-
# The dict can be unpacked and used for the dynamic annotations.
92-
# We also annotate the available custom field identifiers as 'custom_field_keys'.
93-
# Therefore, we know which custom fields are available for this object.
94-
"""
95-
return (
96-
super()
97-
.get_queryset()
98-
.annotate(**fields)
99-
.annotate(
100-
custom_field_keys=ArraySubquery(
101-
available_fields.filter(self.get_type_filter()).values_list(
102-
"identifier", flat=True
94+
"""
95+
# The dict can be unpacked and used for the dynamic annotations.
96+
# We also annotate the available custom field identifiers as 'custom_field_keys'.
97+
# Therefore, we know which custom fields are available for this object.
98+
"""
99+
return (
100+
super()
101+
.get_queryset()
102+
.annotate(**fields)
103+
.annotate(
104+
custom_field_keys=ArraySubquery(
105+
available_fields.filter(self.get_type_filter()).values_list(
106+
"identifier", flat=True
107+
)
103108
)
104109
)
105110
)
106-
)
111+
except (ProgrammingError, RuntimeError):
112+
return super().get_queryset()
107113

108114

109115
class CustomFieldTypeBaseModel(TimeStampedModel):

0 commit comments

Comments
 (0)