Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django prefetch_related vs select_related

class ModelA(models.Model):
    pass

class ModelB(models.Model):
    a = ForeignKey(ModelA)

ModelB.objects.select_related('a').all() # Forward ForeignKey relationship
ModelA.objects.prefetch_related('modelb_set').all() # Reverse ForeignKey relationship
Comment

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()
Comment

prefetch_related list django

# Use the magic syntax with "*" to expand the list
Knight.objects.all().prefetch_related(*selected_attributes)
Comment

django select_related and prefetch_related

django select_related and prefetch_related

select_related >> foreignkey
prefetch_related >> many to many
basically they are performance boosters ,used to avoid multiple unnecessary queries
Both methods achieve the same purpose, to forego unnecessary db queries. 
But they use different approaches for efficiency..
Comment

PREVIOUS NEXT
Code Example
Python :: django cache framework 
Python :: take union of two dataframes pandas 
Python :: how to use inputs in python 
Python :: What Is Python Recursive Function in python 
Python :: conda 
Python :: subtract constant from list 
Python :: python child class init 
Python :: Adding column to CSV Dictreader 
Python :: python add encoding for non-English language like Arabic 
Python :: How to append variable in Python 
Python :: using pickle to create binary files 
Python :: supercharged python 
Python :: codegrepper is cool 
Python :: python russian roulette 
Python :: arcpy save map layer to feature class 
Python :: python print over the same line 
Python :: calc investiment money puthon 
Python :: udp client server chat program in python 
Python :: Update only values in python 
Python :: labelling row in python 
Python :: pip_install_packages2.bat 
Python :: What is the expected value of print to this program X = 2 Y = 3 Z = X + Y print(Y) #Z 
Python :: `nlp.add_pipe` now takes the string name of the registered component factory 
Python :: print all elements of dictionary except one in python 
Python :: libraries used in ANN with Keras Sequential Model 
Python :: python load array 
Python :: somebody please get rid of my annoying-as-hell sunburn!!! 
Python :: python webscrapping downloading all the videos in a playlist 
Python :: django filter word count greater than 
Python :: python time range monthly 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =