|
2 | 2 |
|
3 | 3 | from django.contrib.contenttypes.fields import GenericRelation |
4 | 4 | from django.contrib.postgres.expressions import ArraySubquery |
| 5 | +from django.db import IntegrityError |
5 | 6 | from django.db import models |
| 7 | +from django.db import ProgrammingError |
6 | 8 | from django.db.models import OuterRef |
7 | 9 | from django.db.models import Q |
8 | 10 | from django.db.models import QuerySet |
@@ -76,34 +78,39 @@ def get_queryset(self) -> QuerySet: |
76 | 78 | """ |
77 | 79 | We filter all available custom fields for the current model. |
78 | 80 | """ |
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 | + } |
89 | 94 |
|
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 | + ) |
103 | 109 | ) |
104 | 110 | ) |
105 | 111 | ) |
106 | | - ) |
| 112 | + except (ProgrammingError, RuntimeError, IntegrityError): |
| 113 | + return super().get_queryset() |
107 | 114 |
|
108 | 115 |
|
109 | 116 | class CustomFieldTypeBaseModel(TimeStampedModel): |
|
0 commit comments