Skip to content

Commit fc11b76

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

File tree

1 file changed

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

1 file changed

+31
-24
lines changed

django_features/custom_fields/models/base.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from django.contrib.contenttypes.fields import GenericRelation
44
from django.contrib.postgres.expressions import ArraySubquery
5+
from django.db import IntegrityError
56
from django.db import models
7+
from django.db import ProgrammingError
68
from django.db.models import OuterRef
79
from django.db.models import Q
810
from django.db.models import QuerySet
@@ -76,34 +78,39 @@ def get_queryset(self) -> QuerySet:
7678
"""
7779
We filter all available custom fields for the current model.
7880
"""
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}
81+
try:
82+
available_fields = CustomField.objects.for_model(self.model)
83+
84+
"""
85+
This for loop creates a dict with all available custom field values with a subquery for the specific object.
86+
The dict key is the identifier of the custom field amd the value is the custom value.
87+
If the object has no value for the field, it will return None.
88+
More information can be found in the django documentation:
89+
https://docs.djangoproject.com/en/5.2/ref/models/expressions/#subquery-expressions
90+
"""
91+
fields = {
92+
field.identifier: self._subquery(field) for field in available_fields
93+
}
8994

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
95+
"""
96+
# The dict can be unpacked and used for the dynamic annotations.
97+
# We also annotate the available custom field identifiers as 'custom_field_keys'.
98+
# Therefore, we know which custom fields are available for this object.
99+
"""
100+
return (
101+
super()
102+
.get_queryset()
103+
.annotate(**fields)
104+
.annotate(
105+
custom_field_keys=ArraySubquery(
106+
available_fields.filter(self.get_type_filter()).values_list(
107+
"identifier", flat=True
108+
)
103109
)
104110
)
105111
)
106-
)
112+
except (ProgrammingError, RuntimeError, IntegrityError):
113+
return super().get_queryset()
107114

108115

109116
class CustomFieldTypeBaseModel(TimeStampedModel):

0 commit comments

Comments
 (0)