Files
Feanil Patel 9cf2f9f298 Run 2to3 -f future . -w
This will remove imports from __future__ that are no longer needed.

https://docs.python.org/3.5/library/2to3.html#2to3fixer-future
2019-12-30 10:35:30 -05:00

50 lines
1.2 KiB
Python

""" Utility functions related to database queries """
from django.conf import settings
_READ_REPLICA_DB_ALIAS = "read_replica"
def use_read_replica_if_available(queryset):
"""
If there is a database called 'read_replica',
use that database for the queryset / manager.
Example usage:
queryset = use_read_replica_if_available(SomeModel.objects.filter(...))
Arguments:
queryset (QuerySet)
Returns: QuerySet
"""
return (
queryset.using(_READ_REPLICA_DB_ALIAS)
if _READ_REPLICA_DB_ALIAS in settings.DATABASES
else queryset
)
def read_replica_or_default():
"""
If there is a database called "read_replica",
return "read_replica", otherwise return "default".
This function is similiar to `use_read_replica_if_available`,
but is be more syntactically convenient for method call chaining.
Also, it always falls back to "default",
no matter what the queryset was using before.
Example usage:
queryset = SomeModel.objects.filter(...).using(read_replica_or_default())
Returns: str
"""
return (
_READ_REPLICA_DB_ALIAS
if _READ_REPLICA_DB_ALIAS in settings.DATABASES
else "default"
)