File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,7 +24,8 @@ def decorator(func):
2424 return decorator (function )
2525
2626
27- def display (function = None ):
27+ # TODO: make the sorter parameter a string to specify how to sort the data
28+ def display (function = None , * , sorter : bool = False ):
2829 """Conveniently add attributes to a display function:
2930
3031 Example of usage:
@@ -33,10 +34,13 @@ async def is_published(self, obj):
3334 return obj.publish_date is not None
3435
3536 :param function: A function to decorate.
37+ :param sorter: Enable sorting or not. **WARNING**: supported only for Django and Tortoise.
38+ Function name should be like an ORM ordering param, e.g. `def user__username(self, obj)`.
3639 """
3740
3841 def decorator (func ):
3942 func .is_display = True
43+ func .sorter = sorter
4044 return func
4145
4246 if function is None :
Original file line number Diff line number Diff line change 1515 ModelSchema ,
1616)
1717
18+ try :
19+ from tortoise .models import Model as TortoiseModel
20+ except ImportError :
21+
22+ class TortoiseModel :
23+ pass
24+
25+
26+ try :
27+ from django .db .models import Model as DjangoModel
28+ except ImportError :
29+
30+ class DjangoModel :
31+ pass
32+
1833
1934def register_admin_model_class (admin_model_class : type [ModelAdmin ], orm_model_classes : list [Any ], ** kwargs ) -> None :
2035 """This method is used to register an admin model.
@@ -170,12 +185,16 @@ def generate_models_schema(
170185 continue
171186 column_index = column_fields .index (field_name )
172187
188+ sorter = getattr (display_field_function , "sorter" , False )
189+ if sorter and not issubclass (orm_model_cls , (TortoiseModel , DjangoModel )):
190+ raise NotImplementedError ("Sorter of custom columns is supported only for Tortoise or Django" )
191+
173192 fields_schema .append (
174193 ModelFieldSchema (
175194 name = field_name ,
176195 list_configuration = ListConfigurationFieldSchema (
177196 index = column_index ,
178- sorter = False ,
197+ sorter = sorter ,
179198 is_link = field_name in getattr (admin_model_obj , "list_display_links" , ()),
180199 empty_value_display = admin_model_obj .empty_value_display ,
181200 filter_widget_type = None ,
You can’t perform that action at this time.
0 commit comments