Changes between Initial Version and Version 1 of Ticket #33950
- Timestamp:
- Aug 23, 2022, 5:08:22 AM (2 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #33950 – Description
initial v1 3 3 I have these three models: 4 4 5 ``` 5 {{{ 6 6 class Place(models.Model): 7 7 name = models.CharField(max_length=255) … … 14 14 visit = models.ForeignKey(Visit, on_delete=models.CASCADE, related_name="documents") 15 15 file = models.FileField() 16 ``` 16 }}} 17 17 18 18 A form to edit a visit: 19 19 20 ``` 20 {{{ 21 21 class VisitForm(forms.ModelForm): 22 22 class Meta: … … 31 31 required=False, 32 32 ) 33 ``` 33 }}} 34 34 35 35 And a generic view for the places: 36 36 37 ``` 37 {{{ 38 38 class PlaceView(DetailView): 39 39 queryset = Place.objects.prefetch_related("visits__documents") … … 49 49 def post(self, request, *args, **kwargs): 50 50 // Handle the form 51 ``` 51 }}} 52 52 53 Despite the `prefetch_related("visits__documents")` call in my view, `ModelMultipleChoiceField()` doesn't detect that the documents of the visit (i.e. the form instance) are already fetched because `visit.documents.all()._prefetch_related_lookups`is null: https://github.com/django/django/blob/stable/3.2.x/django/forms/models.py#L116753 Despite the {{{prefetch_related("visits__documents")}}} call in my view, {{{ModelMultipleChoiceField()}}} doesn't detect that the documents of the visit (i.e. the form instance) are already fetched because {{{visit.documents.all()._prefetch_related_lookups}}} is null: https://github.com/django/django/blob/stable/3.2.x/django/forms/models.py#L1167 54 54 55 So is in the view: `self.object.visits.all()._prefetch_related_lookups`is null as well.55 So is in the view: {{{self.object.visits.all()._prefetch_related_lookups}}} is null as well. 56 56 57 57 Many thanks!