File tree Expand file tree Collapse file tree 3 files changed +29
-2
lines changed
Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44The format is based on [ Keep a Changelog] ( https://keepachangelog.com/en/1.0.0/ ) ,
55and this project adheres to [ Semantic Versioning] ( https://semver.org/spec/v2.0.0.html ) .
66
7+ ## [ 0.2.0]
8+
9+ - Add support to nested prefetch lookups like ` v.VirtualModel(manager=User.objects, lookup="course__facilitators") `
10+ * Warning: this will remain undocumented for now, because the behavior is strange:
11+ the prefetch is made inside ` course ` in this case, due to Django behavior.
12+ - Add parameter ` serializer_context ` to be used in ` v.Annotation ` and ` get_prefetch_queryset ` .
13+
714## [ 0.1.6]
815
916- Fix support for custom manager in ` VirtualModel ` initialization
Original file line number Diff line number Diff line change @@ -180,8 +180,8 @@ class Meta:
180180
181181 @hints .defined_on_virtual_model ()
182182 def get_facilitator_users (self , lesson ):
183- if hasattr (lesson , "facilitator_users" ):
184- return list ({u .email for u in lesson .facilitator_users })
183+ if hasattr (lesson . course , "facilitator_users" ):
184+ return list ({u .email for u in lesson .course . facilitator_users })
185185
186186 # this won't run because it's defined on virtual model,
187187 # but one could add fallback code here:
Original file line number Diff line number Diff line change @@ -432,3 +432,23 @@ class Meta:
432432
433433 for completed_lesson in completed_lesson_list :
434434 assert isinstance (completed_lesson .lesson .course .created_by , User )
435+
436+ def test_prefetch_with_nested_lookup (self ):
437+ class VirtualLesson (v .VirtualModel ):
438+ facilitator_users = v .VirtualModel (manager = User .objects , lookup = "course__facilitators" )
439+
440+ class Meta :
441+ model = Lesson
442+
443+ virtual_model = VirtualLesson ()
444+ qs = Lesson .objects .all ()
445+ lookup_list = ["facilitator_users" ]
446+
447+ optimized_qs = virtual_model .get_optimized_queryset (qs = qs , lookup_list = lookup_list )
448+ with self .assertNumQueries (3 ):
449+ lesson_list = list (optimized_qs )
450+ assert len (lesson_list ) == 9
451+
452+ for lesson in lesson_list :
453+ for user in lesson .course .facilitator_users :
454+ assert isinstance (user , User )
You can’t perform that action at this time.
0 commit comments