Debian Codegraph @

guardian/ctypes.py

raw
1 from typing import Any
2
3 from django.contrib.contenttypes.models import ContentType
4 from django.db.models import Model
5 from django.utils.module_loading import import_string
6
7 from guardian.conf import settings as guardian_settings
8
9
10 def get_content_type(obj: Model | type[Model]) -> Any:
11 get_content_type_function = import_string(guardian_settings.GET_CONTENT_TYPE)
12 return get_content_type_function(obj)
13
14
15 def get_default_content_type(obj: Model | type[Model]) -> ContentType:
16 """Get content type for a given object using Django's content type framework.
17
18 Parameters:
19 obj (Model | type[Model]): Object for which content type is to be fetched.
20
21 Returns:
22 Content type for the given object.
23
24 See Also:
25 https://docs.djangoproject.com/en/5.1/ref/contrib/contenttypes/
26
27 """
28 return ContentType.objects.get_for_model(obj)