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 :: ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters. 
Python :: python dictionary dynamic key 
Python :: is python oop 
Python :: python panda append rows to csv python 
Python :: matplotlib animate 
Python :: merge two dataframes based on column 
Python :: pygame get keypress code 
Python :: python dequeu 
Python :: matplotlib python background color 
Python :: import get user model django 
Python :: how to pass parameters in python script 
Python :: install aws sdk python 
Python :: python shuffle array 
Python :: np.arange and np.linspace difference 
Python :: getters and setters in python 
Python :: pandas split column with tuple 
Python :: count most frequent words in list python 
Python :: python make string one line 
Python :: How to select parts of a numpy array 
Python :: pandas convert numbers in parentheses to negative 
Python :: Python NumPy broadcast_to() Function Example 
Python :: python subtract list from list 
Python :: Discord python get member object by id 
Python :: how to make a checksum to a file python 
Python :: requests python3 example 
Python :: convert timedelta to days 
Python :: python cli click 
Python :: if else in list comprehension 
Python :: how to write a code for anagram in python 
Python :: transformer un dictionnaire en liste python 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =