@@ -87,6 +87,7 @@ def __init__(
8787 using = None ,
8888 component_name = None ,
8989 component_id = None ,
90+ component_key = "" ,
9091 frontend_context_variables = {},
9192 init_js = False ,
9293 ** kwargs ,
@@ -103,6 +104,7 @@ def __init__(
103104
104105 self .component_id = component_id
105106 self .component_name = component_name
107+ self .component_key = component_key
106108 self .frontend_context_variables = frontend_context_variables
107109 self .init_js = init_js
108110
@@ -121,13 +123,15 @@ def render(self):
121123 root_element = UnicornTemplateResponse ._get_root_element (soup )
122124 root_element ["unicorn:id" ] = self .component_id
123125 root_element ["unicorn:name" ] = self .component_name
126+ root_element ["unicorn:key" ] = self .component_key
124127 root_element ["unicorn:checksum" ] = checksum
125128
126129 if self .init_js :
127130 script_tag = soup .new_tag ("script" )
128131 init = {
129132 "id" : self .component_id ,
130133 "name" : self .component_name ,
134+ "key" : self .component_key ,
131135 "data" : orjson .loads (self .frontend_context_variables ),
132136 }
133137 init = orjson .dumps (init ).decode ("utf-8" )
@@ -166,6 +170,7 @@ def _desoupify(soup):
166170class UnicornView (TemplateView ):
167171 response_class = UnicornTemplateResponse
168172 component_name : str = ""
173+ component_key : str = ""
169174 request = None
170175
171176 # Caches to reduce the amount of time introspecting the class
@@ -292,6 +297,7 @@ def render(self, init_js=False) -> str:
292297 context = self .get_context_data (),
293298 component_name = self .component_name ,
294299 component_id = self .component_id ,
300+ component_key = self .component_key ,
295301 frontend_context_variables = frontend_context_variables ,
296302 init_js = init_js ,
297303 )
@@ -561,6 +567,7 @@ def _is_public(self, name: str) -> bool:
561567 # Component methods
562568 "component_id" ,
563569 "component_name" ,
570+ "component_key" ,
564571 "reset" ,
565572 "mount" ,
566573 "hydrate" ,
@@ -591,6 +598,7 @@ def _is_public(self, name: str) -> bool:
591598 def create (
592599 component_id : str ,
593600 component_name : str ,
601+ component_key : str = "" ,
594602 use_cache = True ,
595603 request : HttpRequest = None ,
596604 kwargs : Dict [str , Any ] = {},
@@ -602,6 +610,8 @@ def create(
602610 param component_id: Id of the component. Required.
603611 param component_name: Name of the component. Used to locate the correct `UnicornView`
604612 component class and template if necessary. Required.
613+ param component_key: Key of the component to allow multiple components of the same name
614+ to be differentiated. Optional.
605615 param use_cache: Get component from cache or force construction of component. Defaults to `True`.
606616 param kwargs: Keyword arguments for the component passed in from the template. Defaults to `{}`.
607617
@@ -613,13 +623,11 @@ def create(
613623 assert component_name , "Component name is required"
614624
615625 if use_cache :
616- cache_key = f"{ component_name } -{ component_id } "
617-
618- if cache_key in constructed_views_cache :
619- component = constructed_views_cache [cache_key ]
626+ if component_id in constructed_views_cache :
627+ component = constructed_views_cache [component_id ]
620628 component .setup (request )
621629 component ._validate_called = False
622- logger .debug (f"Retrieve { cache_key } from constructed views cache" )
630+ logger .debug (f"Retrieve { component_id } from constructed views cache" )
623631 return component
624632
625633 locations = []
@@ -672,6 +680,7 @@ def _get_component_class(
672680 component = component_class (
673681 component_id = component_id ,
674682 component_name = component_name ,
683+ component_key = component_key ,
675684 request = request ,
676685 ** kwargs ,
677686 )
@@ -680,8 +689,7 @@ def _get_component_class(
680689 component ._validate_called = False
681690
682691 # Put the component in a "cache" to skip looking for the component on the next request
683- cache_key = f"{ component_name } -{ component_id } "
684- constructed_views_cache [cache_key ] = component
692+ constructed_views_cache [component_id ] = component
685693
686694 return component
687695 except ModuleNotFoundError as e :
0 commit comments