Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

django select_related and prefetch_related

* django select_related and prefetch_related
* Prefetch_related and select_related functions in django
---------------------------------
select_related() “follows” foreign-key relationships,
selecting additional related-object data when it executes its query.

prefetch_related() does a separate lookup for each relationship, 
and does the “joining” in Python.
-------------------
Example

class A(models.Model):
pass

class B(models.Model):
a = ForeignKey(ModelA)
# Forward ForeignKey relationship
A.objects.select_related('a').all()
# Reverse ForeignKey relationship
A.objects.prefetch_related('modelb_set').all()
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #django
ADD COMMENT
Topic
Name
8+2 =