Child data table in Django

243 views
Skip to first unread message

Eugene TUYIZERE

unread,
Oct 13, 2021, 8:06:23 AM10/13/21
to django...@googlegroups.com
Dear All,

Kindly assist me to have a tutorial or code to make this kind of table in django. I failed to do this from the datatable .net tutorial.
image.png

regards, 

--
 Eugene

Ammar Mohammed

unread,
Oct 13, 2021, 8:28:54 AM10/13/21
to django...@googlegroups.com
Hello Dear

You can use djangos prefetch_related in the docs :

prefetch_related()

prefetch_related(*lookups)

Returns a QuerySet that will automatically retrieve, in a single batch, related objects for each of the specified lookups.

This has a similar purpose to select_related, in that both are designed to stop the deluge of database queries that is caused by accessing related objects, but the strategy is quite different.

select_related works by creating an SQL join and including the fields of the related object in the SELECT statement. For this reason, select_related gets the related objects in the same database query. However, to avoid the much larger result set that would result from joining across a ‘many’ relationship, select_related is limited to single-valued relationships - foreign key and one-to-one.

prefetch_related, on the other hand, does a separate lookup for each relationship, and does the ‘joining’ in Python. This allows it to prefetch many-to-many and many-to-one objects, which cannot be done using select_related, in addition to the foreign key and one-to-one relationships that are supported by select_related. It also supports prefetching of GenericRelation and GenericForeignKey, however, it must be restricted to a homogeneous set of results. For example, prefetching objects referenced by a GenericForeignKey is only supported if the query is restricted to one ContentType.

For example, suppose you have these models:

from django.db import models

class Topping(models.Model):
    name = models.CharField(max_length=30)

class Pizza(models.Model):
    name = models.CharField(max_length=50)
    toppings = models.ManyToManyField(Topping)

    def __str__(self):              # __unicode__ on Python 2
        return "%s (%s)" % (
            self.name,
            ", ".join(topping.name for topping in self.toppings.all()),
        )

and run:


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABxpZHuY_zoSvcwQL79UPts6g%3DmJRD%3DO_B%2Bc5%3DvinOtPKNLFLw%40mail.gmail.com.

Eugene TUYIZERE

unread,
Oct 13, 2021, 8:43:22 AM10/13/21
to django...@googlegroups.com
I want to make the same as this below but using django. That is what I want. The data are in one table but display shows child table 


any one help please



--
TUYIZERE Eugene

Msc Degree in Mathematical Science

African Institute for Mathematical Sciences (AIMS Cameroon)
Crystal Garden-Lime, Cameroon


Bsc in Computer Science

UR-Nyagatare Campus

Email: eugene....@aims-cameroon.org
           eugenet...@gmail.com

Tel: (+250) 7 88 26 33 38, (+250) 7 22 26 33 38

Sebastian Jung

unread,
Oct 13, 2021, 9:30:22 AM10/13/21
to django...@googlegroups.com
Hello eugene,

You have implement also jquery >= 3.5.1 and datatablea.min.js

Please show us your html code and javascript code that open row when you click on this item...

Regards

Eugene TUYIZERE

unread,
Oct 13, 2021, 11:20:40 AM10/13/21
to django...@googlegroups.com
Dear Sebstian,

Thank you for your email. Frankly speaking I do not have the code for that kind of table. I used to use the usual datatable but now I want to implement the one with child row as the image I attached previously.

Regards, 

Reply all
Reply to author
Forward
0 new messages