Hello DearYou 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 theSELECT
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 usingselect_related
, in addition to the foreign key and one-to-one relationships that are supported byselect_related
. It also supports prefetching ofGenericRelation
andGenericForeignKey
, however, it must be restricted to a homogeneous set of results. For example, prefetching objects referenced by aGenericForeignKey
is only supported if the query is restricted to oneContentType
.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:
>>> Pizza.objects.all()Url : https://docs.djangoproject.com/en/1.10/ref/models/querysets/--On Wed, 13 Oct 2021, 2:06 PM Eugene TUYIZERE, <eugenetuyizere@gmail.com> wrote: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.regards,--Eugene
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+unsubscribe@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.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHs1H7vL%3Dzawt%2BqPiBU1qrZojSLpdjZuMWYiWT3jic-8dZfTEw%40mail.gmail.com.
Crystal Garden-Lime, Cameroon
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABxpZHtwZg6JVSMa4Gbc%2B2cb1obRNs5mxsX95qghNAsM23%2B%2BUg%40mail.gmail.com.
No comments:
Post a Comment