[Answered ]-Scheduling a satutus change using celery in django

1👍

contractList = Contrat.objects.all()
for contrat in contractList:
    if contrat.NbDays<=date.today() and contrat.statut_assurance=='Encours':
        Contrat.objects.filter(id=contract.id).update(statut_assurance='Expiré')
        print('Numéro de contrat est :',contrat.numero_de_contrat,\
            ' et le statut est: ',contrat.statut_assurance)
👤Akram

0👍

You should not update the contractList, since that is a queryset with all records, you update that item with:

def status_schedule(): 
    for contrat in Contrat.objects.filter(statut_assurance='Encours'):
        if contrat.get_NbDays() <= date.today():
            contrat.statut_assurance = 'Expiré'
            contrat.save()
            print('Numéro de contrat est :',contrat.numero_de_contrat,\
                ' et le statut est: ',contrat.statut_assurance)

You should also not break the loop.

Depending on the implementation of get_NbDays, you can also move that to the .filter(…) clause and in that case work with a .update(…)

Leave a comment