Optimizing Django View to Handle Large Data Sets Efficiently

I'm seeking advice on optimizing a Django view to enhance its performance, particularly when dealing with a large number of clients in the database. Here's the scenario:

I have a Django view called tClients that retrieves client data and renders it on a webpage. The view performs filtering based on a query parameter called status. If status is provided and falls within certain values, the view filters clients accordingly.

Here's the current implementation of the view:

def tClients(request):
    client_Form = NewClient()
    client_update_form = ClientUpdateForm()

    filter_status = request.GET.get('status', None)

    if filter_status is not None and filter_status in ['-1', '0', '1']:
        clients = Client.objects.select_related()
        # values('id','nni','nom','date','telephone','address','observation')

        clients = [client for client in clients if client.is_debt == int(filter_status)]
        clients = Client.objects.values('id','nni','nom','date','telephone','address','observation')

    context = {
        'client_Form': client_Form,
        'client_update_form': client_update_form,
        'tClients': clients,

    return render(request, 'clients/list_clients.html', context)

The Client model is defined as follows:

class Client(models.Model):
    nni = models.CharField(max_length=20, verbose_name=_('nni'), unique=True)
    uid = models.CharField(max_length=500)
    nom = models.CharField(max_length=255, verbose_name=_('nom'))
    date = models.DateField(verbose_name=_("date  d'inscription"))
    img = models.ImageField(upload_to='client/qrcodes', blank=True, null=True, verbose_name=_("Image"))
    telephone = models.CharField(max_length=255, null=True, verbose_name=_("Telephone"))
    address = models.CharField(null=True, max_length=255, verbose_name=_("Address"))
    observation = models.TextField(null=True, verbose_name=_("Observation"))
    user = models.ForeignKey(User, blank=True, on_delete=models.SET_NULL, null=True)

    def is_debt(self):
        current_date = timezone.localdate()
        debts = self.client_detter.all()
        if debts.exists():
            for debt in debts:
                duration = current_date - debt.date_dette
                duration_in_days = duration.days
                if duration_in_days >= debt.dette_duration and debt.amount != debt.dette_montant_payee:
                    return -1
                elif duration_in_days < debt.dette_duration and debt.amount != debt.dette_montant_payee:
                    return 0
            return 1
            return 1

Initially, I attempted to enhance query performance by utilizing the values method to select only the required fields. However, I encountered a challenge when attempting to filter the results based on the status parameter.

Since values returns a dictionary, I couldn't directly access the is_debt property, which is defined as a @property within the Client model.

Additionally, I tried annotating the queryset with the is_debt property, but unfortunately, it didn't yield the desired results.

Could anyone provide guidance on how to optimize this query. Thank you .

Back to Top