当前位置: 首页>>代码示例>>Python>>正文


Python router.db_for_read方法代码示例

本文整理汇总了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
            ) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:23,代码来源:related.py

示例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 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:20,代码来源:db.py

示例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
            ) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:23,代码来源:related.py

示例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
            ) 
开发者ID:Yeah-Kun,项目名称:python,代码行数:23,代码来源:related.py

示例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))) 
开发者ID:blackye,项目名称:luscan-devel,代码行数:24,代码来源:db.py

示例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 
开发者ID:blackye,项目名称:luscan-devel,代码行数:19,代码来源:db.py

示例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
            ) 
开发者ID:drexly,项目名称:openhgsenti,代码行数:23,代码来源:related.py

示例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 
开发者ID:adamchainz,项目名称:django-mysql,代码行数:27,代码来源:cache.py

示例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 #
    ####################### 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:8,代码来源:manager.py

示例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))) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:29,代码来源:db.py

示例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 #
    ####################### 
开发者ID:blackye,项目名称:luscan-devel,代码行数:8,代码来源:manager.py

示例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 #
    ################### 
开发者ID:blackye,项目名称:luscan-devel,代码行数:11,代码来源:query.py

示例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) 
开发者ID:blackye,项目名称:luscan-devel,代码行数:5,代码来源:related.py

示例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] 
开发者ID:aaugustin,项目名称:django-sequences,代码行数:28,代码来源:__init__.py

示例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) 
开发者ID:adamchainz,项目名称:django-mysql,代码行数:17,代码来源:cache.py


注:本文中的django.db.router.db_for_read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。