File tree Expand file tree Collapse file tree 3 files changed +20
-10
lines changed Expand file tree Collapse file tree 3 files changed +20
-10
lines changed Original file line number Diff line number Diff line change @@ -162,13 +162,15 @@ def get_template_source_from_exception_info(
162
162
163
163
164
164
def get_name_from_obj (obj : Any ) -> str :
165
- name = obj .__name__ if hasattr (obj , "__name__" ) else obj .__class__ .__name__
166
-
167
- if hasattr (obj , "__module__" ):
168
- module = obj .__module__
169
- name = f"{ module } .{ name } "
170
-
171
- return name
165
+ """Get the best name as `str` from a view or a object."""
166
+ # This is essentially a rewrite of the `django.contrib.admindocs.utils.get_view_name`
167
+ # https://github.com/django/django/blob/9a22d1769b042a88741f0ff3087f10d94f325d86/django/contrib/admindocs/utils.py#L26-L32
168
+ if hasattr (obj , "view_class" ):
169
+ klass = obj .view_class
170
+ return f"{ klass .__module__ } .{ klass .__qualname__ } "
171
+ mod_name = obj .__module__
172
+ view_name = getattr (obj , "__qualname__" , obj .__class__ .__name__ )
173
+ return mod_name + "." + view_name
172
174
173
175
174
176
def getframeinfo (frame : Any , context : int = 1 ) -> inspect .Traceback :
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ Pending
12
12
* Fixed template panel to avoid evaluating ``LazyObject `` when not already
13
13
evaluated.
14
14
* Added support for Django 5.0.
15
+ * Refactor the ``utils.get_name_from_obj `` to simulate the behavior of
16
+ ``django.contrib.admindocs.utils.get_view_name ``.
15
17
16
18
4.2.0 (2023-08-10)
17
19
------------------
Original file line number Diff line number Diff line change @@ -18,18 +18,24 @@ def x():
18
18
return 1
19
19
20
20
res = get_name_from_obj (x )
21
- self .assertEqual (res , "tests.test_utils.x" )
21
+ self .assertEqual (
22
+ res , "tests.test_utils.GetNameFromObjTestCase.test_func.<locals>.x"
23
+ )
22
24
23
25
def test_lambda (self ):
24
26
res = get_name_from_obj (lambda : 1 )
25
- self .assertEqual (res , "tests.test_utils.<lambda>" )
27
+ self .assertEqual (
28
+ res , "tests.test_utils.GetNameFromObjTestCase.test_lambda.<locals>.<lambda>"
29
+ )
26
30
27
31
def test_class (self ):
28
32
class A :
29
33
pass
30
34
31
35
res = get_name_from_obj (A )
32
- self .assertEqual (res , "tests.test_utils.A" )
36
+ self .assertEqual (
37
+ res , "tests.test_utils.GetNameFromObjTestCase.test_class.<locals>.A"
38
+ )
33
39
34
40
35
41
class RenderStacktraceTestCase (unittest .TestCase ):
You can’t perform that action at this time.
0 commit comments