Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
How do I query Wagtail tags without it creating hundreds of queries, n+1 problem?

I have a handful of entries that each has dozens of "tags" when I try to fetch that data it causes hundreds of queries. While using prefetching works on all of my other models and links, it does not seem to work on this. The post below says it has something related to wagtails fake query function but it hasn't been fixed yet. Is there a way to utilize tags where I can fetch them via the api so they don't create hundreds of queries (one for each tag) making them unusable due to their slow response time?

Any help you can provide would be much appreciated.

A similar issue was brought up here but got no answer How to prefetch Wagtail post tags?

Also, when I use prefetch it actually doubles the number of queries

Here is my code:

# serializer.py////////////////////////////////////

# orderable models links
# Custom searializers to add custom display fields
class EventCollectionSerializer(serializers.ModelSerializer):
    class Meta:
         model = CategoryEventCollection
         fields = ['id', 'collection_name', 'collection_event']

class TagSerializer(serializers.ModelSerializer):
    class Meta:
         model = Tag
         fields = ['name', 'id']


# MAIN SERIALIZER PRIMARY FOCUS
class PrimaryFocusSerializer(serializers.HyperlinkedModelSerializer):

     # assign fields to custom serializer
     event_collection = EventCollectionSerializer(many=False)
     tags = TagSerializer(many=True)


class Meta:
    model = PrimaryFocus
    fields = ['id',
              'event_collection',
              'title_major',
              'synopsis',
              'level_ability',
              'page_count',
              'reading_category',
              'book_image',
              'author_first_name',
              'author_last_name',
              'author_dob',
              'tags',
              'purchase_link',
              'source',
              ]



# PRIMARY FOCUS VIEWSET//////////////////////////////////
class PrimaryFocusViewSet(viewsets.ModelViewSet):

    # This snippet just console logs how many queries
    def dispatch(self, *args, **kwargs):
        response = super().dispatch(*args, **kwargs)
        print('Queries Counted: {}'.format(len(connection.queries)))
        return response

# fetch all lexis items to setup queryset
queryset = PrimaryFocus.objects.all()

# set serializer class
serializer_class = PrimaryFocusSerializer

# optimize query with select related and prefetching
def get_queryset(self):
    return (PrimaryFocus.objects
            .select_related('event_collection',)
            .prefetch_related('tags', )
            .all()
            )
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda