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:
>>> Pizza.objects.all()
Url : https://docs.djangoproject.com/en/1.10/ref/models/querysets/
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.
No comments:
Post a Comment