當前位置: 首頁>>代碼示例>>Python>>正文


Python fields.DurationField方法代碼示例

本文整理匯總了Python中django.db.models.fields.DurationField方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.DurationField方法的具體用法?Python fields.DurationField怎麽用?Python fields.DurationField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.db.models.fields的用法示例。


在下文中一共展示了fields.DurationField方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: as_sql

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def as_sql(self, compiler, connection):
        try:
            lhs_output = self.lhs.output_field
        except FieldError:
            lhs_output = None
        try:
            rhs_output = self.rhs.output_field
        except FieldError:
            rhs_output = None
        if (not connection.features.has_native_duration_field and
                ((lhs_output and lhs_output.get_internal_type() == 'DurationField')
                or (rhs_output and rhs_output.get_internal_type() == 'DurationField'))):
            return DurationExpression(self.lhs, self.connector, self.rhs).as_sql(compiler, connection)
        expressions = []
        expression_params = []
        sql, params = compiler.compile(self.lhs)
        expressions.append(sql)
        expression_params.extend(params)
        sql, params = compiler.compile(self.rhs)
        expressions.append(sql)
        expression_params.extend(params)
        # order of precedence
        expression_wrapper = '(%s)'
        sql = connection.ops.combine_expression(self.connector, expressions)
        return expression_wrapper % sql, expression_params 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:27,代碼來源:expressions.py

示例2: _combine

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def _combine(self, other, connector, reversed, node=None):
        if not hasattr(other, 'resolve_expression'):
            # everything must be resolvable to an expression
            if isinstance(other, datetime.timedelta):
                other = DurationValue(other, output_field=fields.DurationField())
            else:
                other = Value(other)

        if reversed:
            return CombinedExpression(other, connector, self)
        return CombinedExpression(self, connector, other)

    #############
    # OPERATORS #
    ############# 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:17,代碼來源:expressions.py

示例3: compile

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def compile(self, side, compiler, connection):
        if not isinstance(side, DurationValue):
            try:
                output = side.output_field
            except FieldError:
                pass
            else:
                if output.get_internal_type() == 'DurationField':
                    sql, params = compiler.compile(side)
                    return connection.ops.format_for_duration_arithmetic(sql), params
        return compiler.compile(side) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:13,代碼來源:expressions.py

示例4: _combine

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def _combine(self, other, connector, reversed):
        if not hasattr(other, 'resolve_expression'):
            # everything must be resolvable to an expression
            if isinstance(other, datetime.timedelta):
                other = DurationValue(other, output_field=fields.DurationField())
            else:
                other = Value(other)

        if reversed:
            return CombinedExpression(other, connector, self)
        return CombinedExpression(self, connector, other)

    #############
    # OPERATORS #
    ############# 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:17,代碼來源:expressions.py

示例5: as_sql

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def as_sql(self, compiler, connection):
        try:
            lhs_output = self.lhs.output_field
        except FieldError:
            lhs_output = None
        try:
            rhs_output = self.rhs.output_field
        except FieldError:
            rhs_output = None
        if (not connection.features.has_native_duration_field and
                ((lhs_output and lhs_output.get_internal_type() == 'DurationField') or
                 (rhs_output and rhs_output.get_internal_type() == 'DurationField'))):
            return DurationExpression(self.lhs, self.connector, self.rhs).as_sql(compiler, connection)
        if (lhs_output and rhs_output and self.connector == self.SUB and
            lhs_output.get_internal_type() in {'DateField', 'DateTimeField', 'TimeField'} and
                lhs_output.get_internal_type() == rhs_output.get_internal_type()):
            return TemporalSubtraction(self.lhs, self.rhs).as_sql(compiler, connection)
        expressions = []
        expression_params = []
        sql, params = compiler.compile(self.lhs)
        expressions.append(sql)
        expression_params.extend(params)
        sql, params = compiler.compile(self.rhs)
        expressions.append(sql)
        expression_params.extend(params)
        # order of precedence
        expression_wrapper = '(%s)'
        sql = connection.ops.combine_expression(self.connector, expressions)
        return expression_wrapper % sql, expression_params 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:31,代碼來源:expressions.py

示例6: __init__

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def __init__(self, lhs, rhs):
        super(TemporalSubtraction, self).__init__(lhs, self.SUB, rhs, output_field=fields.DurationField()) 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:4,代碼來源:expressions.py

示例7: _front_page

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def _front_page(paging_size=settings.PAGING_SIZE, page=0, add_filter={}, add_q=[], as_of=None, days_back=50):
    # TODO: weighting https://medium.com/hacking-and-gonzo/how-hacker-news-ranking-algorithm-works-1d9b0cf2c08d
    # (P-1) / (T+2)^G
    if as_of is None:
        now = timezone.now()
    else:
        now = as_of
    if connection.vendor == 'postgresql':
        now_value = Value(now, output_field=fields.DateTimeField())
        submission_age_float = ExpressionWrapper(  ( now_value - F('created_at')), output_field=fields.DurationField())
        submission_age_hours = ExpressionWrapper(Extract(F('tf'), 'epoch') / 60 / 60 + 2.1 , output_field=fields.FloatField())
        real_p = ExpressionWrapper(F('points') - 1, output_field=fields.FloatField())
        formula = ExpressionWrapper(   F('p') / ( Power(F('tfh'), F('g'))  +0.001)   , output_field=fields.FloatField())
        return Story.objects.select_related('user')\
                .filter(duplicate_of__isnull=True)\
                .filter(points__gte=1) \
                .filter(created_at__gte=now - datetime.timedelta(days=days_back)) \
                .filter(created_at__lte=now) \
                .filter(**add_filter) \
                .annotate(tf=submission_age_float) \
                .annotate(tfh=submission_age_hours) \
                .annotate(p=real_p) \
                .annotate(g=Value(1.8, output_field=fields.FloatField())) \
                .annotate(formula=formula) \
                .order_by('-formula')[(page*paging_size):(page+1)*(paging_size)]
    elif connection.vendor == 'sqlite':
        now_value = Value(now, output_field=fields.DateTimeField())
        submission_age_float = ExpressionWrapper(  ( now_value - F('created_at')), output_field=fields.FloatField())
        submission_age_hours = ExpressionWrapper(F('tf') / 60 / 60 / 1000000 + 2.1 , output_field=fields.FloatField())
        real_p = ExpressionWrapper(F('points') - 1, output_field=fields.FloatField())
        formula = ExpressionWrapper(   F('p') / ( Power(F('tfh'), F('g'))  +0.001)   , output_field=fields.FloatField())
        return Story.objects.select_related('user')\
                .filter(duplicate_of__isnull=True)\
                .filter(points__gte=1) \
                .filter(created_at__gte=now - datetime.timedelta(days=days_back)) \
                .filter(created_at__lte=now) \
                .filter(**add_filter) \
                .annotate(tf=submission_age_float) \
                .annotate(tfh=submission_age_hours) \
                .annotate(p=real_p) \
                .annotate(g=Value(1.8, output_field=fields.FloatField())) \
                .annotate(formula=formula) \
                .order_by('-formula')[(page*paging_size):(page+1)*(paging_size)]
    else: 
        raise NotImplementedError("No frontpage magic for database engine %s implemented"%(connection.vendor)) 
開發者ID:sebst,項目名稱:pythonic-news,代碼行數:47,代碼來源:views.py

示例8: overview

# 需要導入模塊: from django.db.models import fields [as 別名]
# 或者: from django.db.models.fields import DurationField [as 別名]
def overview(request, event_url_name):
    event = get_object_or_404(Event, url_name=event_url_name)

    # permission
    if not event.is_admin(request.user):
        return nopermission(request)

    num_helpers = event.helper_set.count()

    num_coordinators = event.all_coordinators.count()

    num_vegetarians = event.helper_set.filter(vegetarian=True).count()

    num_shift_slots = Shift.objects.filter(job__event=event).aggregate(
        Sum('number'))['number__sum']

    empty_slots_expr = ExpressionWrapper(F('number') - F('num_helpers'),
                                         output_field=fields.IntegerField())
    num_empty_shift_slots = Shift.objects.filter(job__event=event) \
        .annotate(num_helpers=Count('helper')) \
        .annotate(empty_slots=empty_slots_expr) \
        .aggregate(Sum('empty_slots'))['empty_slots__sum']

    total_duration = ExpressionWrapper((F('end') - F('begin')) * F('number'),
                                       output_field=fields.DurationField())
    try:
        hours_total = Shift.objects.filter(job__event=event) \
                           .annotate(duration=total_duration) \
                           .aggregate(Sum('duration'))['duration__sum']
    except (OperationalError, OverflowError):
        hours_total = None
    except Exception as e:
        # handle psycopg2.DataError without importing psycopg2
        # happens on overflow with postgresql
        if 'DataError' in str(e.__class__):
            hours_total = None
        else:
            raise e

    # render
    context = {'event': event,
               'num_helpers': num_helpers,
               'num_coordinators': num_coordinators,
               'num_vegetarians': num_vegetarians,
               'num_shift_slots': num_shift_slots,
               'num_empty_shift_slots': num_empty_shift_slots,
               'hours_total': hours_total}
    return render(request, 'statistic/overview.html', context) 
開發者ID:helfertool,項目名稱:helfertool,代碼行數:50,代碼來源:overview.py


注:本文中的django.db.models.fields.DurationField方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。