#33011: my_model.manyrelation_set.all() return empty value
-------------------------------------+-------------------------------------
     Reporter:  Floréal Cabanettes   |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  model set querying   |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Floréal Cabanettes):

 Hi,

 I found the commit responsible for this bug:

 {{{
 036f160733 ("Refs #20577 -- Deferred filtering of prefetched related
 querysets by reverse m2o relation.", 2020-10-06)
 }}}


 The line which cause the problem is on the file
 django/db/models/fields/related_descriptors.py, line 584 (581 at the
 commit, 584 on tag 3.2.6) :

 {{{
 queryset._defer_next_filter = True
 }}}

 If I comment this line, the bug disappear.

 My code is not public (is for my enterprise, not my choice). I try to
 describe a similar architecture bellow:

 I have 2 models, one is "Run", the other "Experiment", related by a
 OneToMany relationship:

 {{{
 class Run(model.Model):
     label = models.CharField(max_length=100, null=False, blank=False)
     owner = models.ForeignKey(User, null=True, blank=True,
 on_delete=models.PROTECT, related_name="run_owner")


 class Experiment(model.Model):
     run = models.ForeignKey(Run, null=False, on_delete=models.PROTECT)
     index1 = models.ForeignKey(Index, null=False,
 on_delete=models.PROTECT, related_name="index1")
     index2 = models.ForeignKey(Index, null=True, blank=True,
 on_delete=models.PROTECT, related_name="index2")
 }}}


 I use the django rest framework to call the server through REST API. For
 one endpoint, I get a run, and I try to get it's experiments, to then make
 a copy of them:

 {{{
 @action(detail=True, methods=["post"])
 def copy(self, request, pk=None):
     try:
         my_run = self.get_queryset().get(pk=pk)
     except ObjectDoesNotExist:
         return Response({"detail": _("Run with id '%s' does not exists") %
 pk},
                         status=status.HTTP_404_NOT_FOUND)
     my_experiments = my_run.experiment_set.all()
     for my_experiment in my_experiments:
          my_experiment.pk = None
          ...
          my_experiment.save()
 }}}

 and the problem is that `my_experiments` is empty with django 3.2.6 (and
 no more empty with the line commented like said before).

 I hope this will help to solve the problem...

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33011#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.96f63c03361a7a8ff2daa92e339c39c3%40djangoproject.com.

Reply via email to