@@ -43,17 +43,24 @@ def _symbol_name(column_name, is_asc):
4343
4444
4545class EnumValue (str ):
46- '''Subclass of str that stores a string value and the sort order of the column'''
47- def __new__ (cls , str_value , order ):
46+ '''Subclass of str that stores a string and an arbitrary value in the "value" property'''
47+
48+ def __new__ (cls , str_value , value ):
4849 return super (EnumValue , cls ).__new__ (cls , str_value )
4950
50- def __init__ (self , str_value , order ):
51+ def __init__ (self , str_value , value ):
5152 super (EnumValue , self ).__init__ ()
52- self .order = order
53+ self .value = value
54+
55+
56+ # Cache for the generated enums, to avoid name clash
57+ _ENUM_CACHE = {}
5358
5459
5560def _sort_enum_for_model (cls , name = None , symbol_name = _symbol_name ):
5661 name = name or cls .__name__ + 'SortEnum'
62+ if name in _ENUM_CACHE :
63+ return _ENUM_CACHE [name ]
5764 items = []
5865 default = []
5966 for column in inspect (cls ).columns .values ():
@@ -64,7 +71,9 @@ def _sort_enum_for_model(cls, name=None, symbol_name=_symbol_name):
6471 if column .primary_key :
6572 default .append (asc_value )
6673 items .extend (((asc_name , asc_value ), (desc_name , desc_value )))
67- return Enum (name , items ), default
74+ enum = Enum (name , items )
75+ _ENUM_CACHE [name ] = (enum , default )
76+ return enum , default
6877
6978
7079def sort_enum_for_model (cls , name = None , symbol_name = _symbol_name ):
@@ -89,7 +98,7 @@ def sort_enum_for_model(cls, name=None, symbol_name=_symbol_name):
8998
9099
91100def sort_argument_for_model (cls , has_default = True ):
92- '''Returns an Graphene argument for the sort field that accepts a list of sorting directions for a model.
101+ '''Returns a Graphene argument for the sort field that accepts a list of sorting directions for a model.
93102 If `has_default` is True (the default) it will sort the result by the primary key(s)
94103 '''
95104 enum , default = _sort_enum_for_model (cls )
0 commit comments