|
1 | | -from django.contrib import admin |
2 | | -from django.forms.models import modelform_factory |
| 1 | +from django.contrib import admin |
| 2 | +from django.forms.models import modelform_factory |
| 3 | +from django.utils.translation import gettext_lazy as _ |
3 | 4 |
|
4 | 5 | # Imports for Dynamic app registrations |
5 | 6 | from django.apps import apps |
6 | 7 |
|
7 | | -# Models |
8 | | -from web.models import * |
9 | | - |
10 | | -# Json Widget |
11 | | -# from web.widget import JsonEditorWidget |
12 | 8 |
|
| 9 | +# Common Model |
| 10 | +try: |
| 11 | + from .models import CommonModel |
| 12 | + COMMON_MODEL_AVAILABLE = True |
| 13 | +except ImportError: |
| 14 | + COMMON_MODEL_AVAILABLE = False |
13 | 15 |
|
14 | 16 |
|
15 | 17 | # CONFIG CONSTANTS |
|
21 | 23 | # ADMIN.PY MAIN CODE |
22 | 24 | class GenericStackedAdmin(admin.StackedInline): |
23 | 25 | extra = 1 |
24 | | - |
25 | 26 | # This method ensures the field order is correct for inlines as well |
26 | 27 | def get_formset(self, request, obj=None, **kwargs): |
27 | | - formset = super().get_formset(request, obj, **kwargs) |
28 | | - form = formset.form |
29 | | - custom_order = [field for field in form.base_fields if field not in CommonModel._meta.fields] |
30 | | - custom_order += [field for field in CommonModel._meta.fields if field in form.base_fields] |
31 | | - form.base_fields = {field: form.base_fields[field] for field in custom_order} |
| 28 | + formset = super().get_formset(request, obj, **kwargs) |
| 29 | + form = formset.form |
| 30 | + custom_order = [field for field in form.base_fields] |
| 31 | + |
| 32 | + if COMMON_MODEL_AVAILABLE: |
| 33 | + custom_order = [field for field in form.base_fields if field not in CommonModel._meta.fields] |
| 34 | + custom_order += [field for field in CommonModel._meta.fields if field in form.base_fields] |
| 35 | + |
| 36 | + form.base_fields = {field: form.base_fields[field] for field in custom_order} |
32 | 37 | return formset |
33 | 38 |
|
34 | 39 |
|
@@ -68,32 +73,17 @@ def __init__(self, model, admin_site): |
68 | 73 |
|
69 | 74 | super().__init__(model, admin_site) |
70 | 75 |
|
71 | | - def formfield_for_dbfield(self, db_field, request, **kwargs): |
72 | | - # Check if the field is a JSONField |
73 | | - if isinstance(db_field, models.JSONField) and self.admin_meta: |
74 | | - # Retrieve the schema configuration for JSON fields |
75 | | - json_fields_meta = self.model.admin_meta.get('json_fields', {}) |
76 | | - |
77 | | - # Retrieve the schema for the specific field, if defined |
78 | | - json_schema = json_fields_meta.get(db_field.name, {}).get('schema') |
79 | | - |
80 | | - if json_schema: |
81 | | - # Initialize the custom widget with the specified schema |
82 | | - kwargs['widget'] = JsonEditorWidget(schema=json_schema) |
83 | | - else: |
84 | | - # Else load the django-jsoneditor widget |
85 | | - kwargs['widget'] = JSONEditor() |
86 | | - |
87 | | - return super().formfield_for_dbfield(db_field, request, **kwargs) |
88 | | - |
89 | 76 | # Function to get the fieldsets |
90 | 77 | def get_fieldsets(self, request, obj=None): |
91 | 78 | if 'fieldsets' in self.admin_meta: |
92 | 79 | return self.admin_meta['fieldsets'] |
93 | 80 |
|
94 | 81 | common_fields = [] |
95 | | - if issubclass(self.model, CommonModel): |
96 | | - common_fields = [field.name for field in CommonModel._meta.fields if field.editable] |
| 82 | + try: |
| 83 | + if issubclass(self.model, CommonModel): |
| 84 | + common_fields = [field.name for field in CommonModel._meta.fields if field.editable] |
| 85 | + except: |
| 86 | + common_fields = [] |
97 | 87 |
|
98 | 88 | other_fields = [field.name for field in self.model._meta.fields if field.name not in common_fields and field.editable and field.name != 'id'] |
99 | 89 | m2m_fields = [field.name for field in self.model._meta.many_to_many] |
|
0 commit comments