Django 1.10, make an update every time an item is searched

0

I am trying to make an attribute increment by 1 every time I search for an item in the database. For example:

criterio = "Alberto"
resultado = Personas.objects.filter(nombre = criterio)
for persona in resultado:
    persona.coincidencias += 1
    persona.save()

I want to know if there is a neater and quicker way to do it, since this method degrades performance remarkably (in practice that query can throw me hundreds or even thousands of results)

    
asked by Genarito 03.01.2017 в 15:22
source

1 answer

2

You can use the update () method in conjunction with F: link
link

from django.db.models import F

criterio = "Alberto"
resultado = Personas.objects.filter(nombre=criterio)
resultado.update(coincidencias=F('coincidencias') + 1)
    
answered by 03.01.2017 / 15:39
source