简体   繁体   中英

Annotated field is not accessible from prefetched model

I have two models as:

class Training(models.Model):
    """Training records Model."""


class PersonTraining(models.Model):
    """Saves the trainings, the employee has completed or is enrolled in."""

    employee = models.ForeignKey(Person, related_name='records', on_delete=models.CASCADE)
    training = models.ForeignKey(Training, related_name='person_training', on_delete=models.CASCADE)

in the get_queryset method of the PersonTrainingView(viewsets.ModelViewSet) i have annotated Training model's objects with 'average_rating' and 'is_top_rated' fields

and then i have prefetched the training model in PersonTraining queryset as:

person_training_queryset = person_training_queryset.prefetch_related(
                              Prefetch('training', queryset=annotated_trainings)
                           )

The issue is that i need to sort the PersonTraining queryset based on the annotated fields of the prefetched Training model as:

person_training_queryset = person_training_queryset.order_by('-training__is_top_rated', '-training__average_rating')

But it is not accessible during execution in the get_queryset method and throws,

django.core.exceptions.FieldError: Cannot resolve keyword

kindly suggest a solution.

note: I am not annotating PersonTraining queryset with 'average_rating' and 'is_top_rated' fields, as i need to apply distinct on the PersonTraining queryset and it will through

'Error: annotate() + distinct(fields) is not implemented.'

+1 Faced the same problem. The queryset that I'm going to use in Prefetch class works as expected with annotated fields by itself, but not in final queryset where it is prefetched. So nested objects haven't annotated attributes which are needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM