Extend has_feature() to accept a model or OT/CT

This commit is contained in:
Jeremy Stretch 2025-07-24 09:29:27 -04:00
parent 9f2ef2b909
commit 943f98e86d

View File

@ -654,14 +654,19 @@ def get_model_features(model):
] ]
def has_feature(model, feature): def has_feature(model_or_ct, feature):
""" """
Returns True if the model supports the specified feature. Returns True if the model supports the specified feature.
""" """
# Resolve a ContentType to its model class # If an ObjectType was passed, we can use it directly
if type(model) is ContentType: if type(model_or_ct) is ObjectType:
model = model.model_class() ot = model_or_ct
ot = ObjectType.objects.get_for_model(model) # If a ContentType was passed, resolve its model class
elif type(model_or_ct) is ContentType:
ot = ObjectType.objects.get_for_model(model_or_ct.model_class())
# For anything else, look up the ObjectType
else:
ot = ObjectType.objects.get_for_model(model_or_ct)
return feature in ot.features return feature in ot.features