本文整理汇总了Python中django.db.router.db_for_read方法的典型用法代码示例。如果您正苦于以下问题:Python router.db_for_read方法的具体用法?Python router.db_for_read怎么用?Python router.db_for_read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.router
的用法示例。
在下文中一共展示了router.db_for_read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def validate(self, value, model_instance):
if self.rel.parent_link:
return
super(ForeignKey, self).validate(value, model_instance)
if value is None:
return
using = router.db_for_read(model_instance.__class__, instance=model_instance)
qs = self.rel.to._default_manager.using(using).filter(
**{self.rel.field_name: value}
)
qs = qs.complex_filter(self.get_limit_choices_to())
if not qs.exists():
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={
'model': self.rel.to._meta.verbose_name, 'pk': value,
'field': self.rel.field_name, 'value': value,
}, # 'pk' is included for backwards compatibility
)
示例2: has_key
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def has_key(self, key, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_read(self.cache_model_class)
table = connections[db].ops.quote_name(self._table)
if settings.USE_TZ:
now = datetime.utcnow()
else:
now = datetime.now()
now = now.replace(microsecond=0)
with connections[db].cursor() as cursor:
cursor.execute("SELECT cache_key FROM %s "
"WHERE cache_key = %%s and expires > %%s" % table,
[key, connections[db].ops.value_to_db_datetime(now)])
return cursor.fetchone() is not None
示例3: validate
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def validate(self, value, model_instance):
if self.remote_field.parent_link:
return
super().validate(value, model_instance)
if value is None:
return
using = router.db_for_read(self.remote_field.model, instance=model_instance)
qs = self.remote_field.model._default_manager.using(using).filter(
**{self.remote_field.field_name: value}
)
qs = qs.complex_filter(self.get_limit_choices_to())
if not qs.exists():
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={
'model': self.remote_field.model._meta.verbose_name, 'pk': value,
'field': self.remote_field.field_name, 'value': value,
}, # 'pk' is included for backwards compatibility
)
示例4: validate
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def validate(self, value, model_instance):
if self.remote_field.parent_link:
return
super(ForeignKey, self).validate(value, model_instance)
if value is None:
return
using = router.db_for_read(self.remote_field.model, instance=model_instance)
qs = self.remote_field.model._default_manager.using(using).filter(
**{self.remote_field.field_name: value}
)
qs = qs.complex_filter(self.get_limit_choices_to())
if not qs.exists():
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={
'model': self.remote_field.model._meta.verbose_name, 'pk': value,
'field': self.remote_field.field_name, 'value': value,
}, # 'pk' is included for backwards compatibility
)
示例5: get
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def get(self, key, default=None, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_read(self.cache_model_class)
table = connections[db].ops.quote_name(self._table)
cursor = connections[db].cursor()
cursor.execute("SELECT cache_key, value, expires FROM %s "
"WHERE cache_key = %%s" % table, [key])
row = cursor.fetchone()
if row is None:
return default
now = timezone.now()
if row[2] < now:
db = router.db_for_write(self.cache_model_class)
cursor = connections[db].cursor()
cursor.execute("DELETE FROM %s "
"WHERE cache_key = %%s" % table, [key])
transaction.commit_unless_managed(using=db)
return default
value = connections[db].ops.process_clob(row[1])
return pickle.loads(base64.b64decode(force_bytes(value)))
示例6: has_key
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def has_key(self, key, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_read(self.cache_model_class)
table = connections[db].ops.quote_name(self._table)
cursor = connections[db].cursor()
if settings.USE_TZ:
now = datetime.utcnow()
else:
now = datetime.now()
now = now.replace(microsecond=0)
cursor.execute("SELECT cache_key FROM %s "
"WHERE cache_key = %%s and expires > %%s" % table,
[key, connections[db].ops.value_to_db_datetime(now)])
return cursor.fetchone() is not None
示例7: validate
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def validate(self, value, model_instance):
if self.remote_field.parent_link:
return
super(ForeignKey, self).validate(value, model_instance)
if value is None:
return
using = router.db_for_read(model_instance.__class__, instance=model_instance)
qs = self.remote_field.model._default_manager.using(using).filter(
**{self.remote_field.field_name: value}
)
qs = qs.complex_filter(self.get_limit_choices_to())
if not qs.exists():
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={
'model': self.remote_field.model._meta.verbose_name, 'pk': value,
'field': self.remote_field.field_name, 'value': value,
}, # 'pk' is included for backwards compatibility
)
示例8: get_many
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def get_many(self, keys, version=None):
made_key_to_key = {self.make_key(key, version=version): key for key in keys}
made_keys = list(made_key_to_key.keys())
for key in made_keys:
self.validate_key(key)
db = router.db_for_read(self.cache_model_class)
table = connections[db].ops.quote_name(self._table)
with connections[db].cursor() as cursor:
cursor.execute(
self._get_many_query.format(
table=table, list_sql=get_list_sql(made_keys)
),
made_keys + [self._now()],
)
rows = cursor.fetchall()
data = {}
for made_key, value, value_type in rows:
key = made_key_to_key[made_key]
data[key] = self.decode(value, value_type)
return data
示例9: db
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def db(self):
return self._db or router.db_for_read(self.model, **self._hints)
#######################
# PROXIES TO QUERYSET #
#######################
示例10: get
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def get(self, key, default=None, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_read(self.cache_model_class)
table = connections[db].ops.quote_name(self._table)
with connections[db].cursor() as cursor:
cursor.execute("SELECT cache_key, value, expires FROM %s "
"WHERE cache_key = %%s" % table, [key])
row = cursor.fetchone()
if row is None:
return default
now = timezone.now()
expires = row[2]
if connections[db].features.needs_datetime_string_cast and not isinstance(expires, datetime):
# Note: typecasting is needed by some 3rd party database backends.
# All core backends work without typecasting, so be careful about
# changes here - test suite will NOT pick regressions here.
expires = typecast_timestamp(str(expires))
if expires < now:
db = router.db_for_write(self.cache_model_class)
with connections[db].cursor() as cursor:
cursor.execute("DELETE FROM %s "
"WHERE cache_key = %%s" % table, [key])
return default
value = connections[db].ops.process_clob(row[1])
return pickle.loads(base64.b64decode(force_bytes(value)))
示例11: db
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def db(self):
return self._db or router.db_for_read(self.model)
#######################
# PROXIES TO QUERYSET #
#######################
示例12: db
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def db(self):
"Return the database that will be used if this query is executed now"
if self._for_write:
return self._db or router.db_for_write(self.model)
return self._db or router.db_for_read(self.model)
###################
# PRIVATE METHODS #
###################
示例13: get_query_set
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def get_query_set(self, **db_hints):
db = router.db_for_read(self.related.model, **db_hints)
return self.related.model._base_manager.using(db)
示例14: get_last_value
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def get_last_value(
sequence_name='default',
*,
using=None
):
"""
Return the last value for a given sequence.
"""
# Inner import because models cannot be imported before their application.
from .models import Sequence
if using is None:
using = router.db_for_read(Sequence)
connection = connections[using]
db_table = connection.ops.quote_name(Sequence._meta.db_table)
with connection.cursor() as cursor:
cursor.execute(
SELECT.format(db_table=db_table),
[sequence_name]
)
result = cursor.fetchone()
return None if result is None else result[0]
示例15: get
# 需要导入模块: from django.db import router [as 别名]
# 或者: from django.db.router import db_for_read [as 别名]
def get(self, key, default=None, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_read(self.cache_model_class)
table = connections[db].ops.quote_name(self._table)
with connections[db].cursor() as cursor:
cursor.execute(self._get_query.format(table=table), (key, self._now()))
row = cursor.fetchone()
if row is None:
return default
else:
value, value_type = row
return self.decode(value, value_type)