1- __all__ = ["get_custom_field_model" , "get_custom_value_model" ]
1+ __all__ = [
2+ "get_custom_field_model" ,
3+ "get_custom_value_model" ,
4+ "clear_custom_field_model_cache" ,
5+ ]
26
37
8+ from functools import lru_cache
9+
410from django .apps import apps as django_apps
511from django .conf import settings
12+ from django .contrib .auth import get_user_model
613from django .core .exceptions import ImproperlyConfigured
714
815from django_features .custom_fields .models .field import AbstractBaseCustomField
916from django_features .custom_fields .models .value import AbstractBaseCustomValue
1017
1118
12- def get_custom_field_model () -> AbstractBaseCustomField :
19+ @lru_cache (maxsize = 1 )
20+ def get_custom_field_model () -> type [AbstractBaseCustomField ]:
21+
22+ get_user_model ()
23+
1324 """
1425 Return the CustomField model that is active in this project.
1526 """
@@ -31,7 +42,8 @@ def get_custom_field_model() -> AbstractBaseCustomField:
3142 )
3243
3344
34- def get_custom_value_model () -> AbstractBaseCustomValue :
45+ @lru_cache (maxsize = 1 )
46+ def get_custom_value_model () -> type [AbstractBaseCustomValue ]:
3547 """
3648 Return the CustomValue model that is active in this project.
3749 """
@@ -53,3 +65,11 @@ def get_custom_value_model() -> AbstractBaseCustomValue:
5365 "CUSTOM_FIELD_VALUE_MODEL refers to model '%s' that has not been installed"
5466 % settings .CUSTOM_FIELD_VALUE_MODEL
5567 )
68+
69+
70+ def clear_custom_field_model_cache () -> None :
71+ """
72+ Clear cached model lookups for custom fields.
73+ """
74+ get_custom_field_model .cache_clear ()
75+ get_custom_value_model .cache_clear ()
0 commit comments