- put methods that apply to a single instance of a Model on the model class itself
- put methods meant to query against a Models entire table that models corresponding managers.py file.
- if a models.py file gets too long to manage then create a models directory and place an __init__ file in it.
- try to avoid using signals, but if you must then put them in a signals.py file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def batch_query_set(query_set, single=False, batch_size=1000): | |
try: | |
last_id = query_set.order_by('-id')[0].id | |
first_id = query_set.order_by('id')[0].id | |
for start in range(first_id, last_id + 1, batch_size): | |
end = min(start + batch_size - 1, last_id) | |
records = query_set.filter(id__range=(start, end)).all() | |
if single: | |
for record in records: | |
yield record |