How to Lazy load datatable with client sided processing?

Viewed 699

I have a table with 3500 entries and foreign keys.

I'm using client-sided processing datatables with: django-rest-framework and Ajax.

It takes up to 10 seconds to load.

Is there a way to show the first 10 entries (first page results) - for the user not to think that my website is broken, because it's taking too long - while the rest of the entries loads in the background?

Also looking for optimizations for the load speed. Thank you for your time.

EDIT: models.py

class Bibrest51(models.Model):
    cadastro_id = models.AutoField(primary_key=True)
    autor = models.CharField(db_column='Autor', max_length=255, blank=True, null=True)
    tema = models.ForeignKey('BibTema', models.DO_NOTHING, blank=True, null=True)

    tipo = models.ForeignKey('Tipo', models.DO_NOTHING, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'bibrest51'
    @property
    def bib_tipo_nome(self):
        return self.tipo.tipo_nome

class BibTema(models.Model):
    tema_id = models.AutoField(primary_key=True)
    tema_nome = models.CharField(max_length=150, blank=True, null=True)
    class Meta:
        managed = False
        db_table = 'tema'
    def __str__(self):
        return self.tema_nome

serializers.py

class TemaSerializer(serializers.ModelSerializer):
    class Meta:
        model = BibTema
        fields = '__all__'   
        depth = 1

class TipoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tipo
        fields = '__all__'
        depth= 1

class BibSerializer(serializers.ModelSerializer):
    temas = TemaSerializer(read_only=True)
    tipos = TipoSerializer(read_only=True)
    class Meta:
        model = Bibrest51
        fields = (
            'autor', 'ano', 'titulo', 'referencia','tipos','temas'
            
        ) 
1 Answers

For optimization you can use:

Is there a way to show the first 10 entries (first page results) - for the user not to think that my website is broken, because it's taking too long - while the rest of the entries loads in the background?

You can implement this with pagination. Request the first 10 records and if the total number of records is more than 10, then request the rest in the background

UPDATED

For serializer try this:

class TemaSerializer(serializers.ModelSerializer):
    class Meta:
        model = BibTema
        fields = '__all__'   
        # depth= 1   # delete string

class TipoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tipo
        fields = '__all__'
        # depth= 1   # delete string

class BibSerializer(serializers.ModelSerializer):
    temas = TemaSerializer(read_only=True, sourse='tema')
    tipos = TipoSerializer(read_only=True, sourse='tipo')
    class Meta:
        model = Bibrest51
        # There are fewer fields in the code you provided, 
        # so I rely on you for this.
        fields = (
            'autor', 'ano', 'titulo', 'referencia','tipos','temas'
            
        ) 
Related