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


Python encoding.force_str方法代碼示例

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


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

示例1: change

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def change(request, object, message_or_fields):
    """
    Log that an object has been successfully changed.

    The argument *message_or_fields* must be a sequence of modified field names
    or a custom change message.
    """
    if isinstance(message_or_fields, str):
        message = message_or_fields
    else:
        message = get_change_message(message_or_fields)
    models.LogEntry.objects.log_action(
        user_id=request.user.pk,
        content_type_id=ContentType.objects.get_for_model(object).pk,
        object_id=object.pk,
        object_repr=force_str(object),
        action_flag=models.CHANGE,
        change_message=message,
    ) 
開發者ID:GamesDoneQuick,項目名稱:donation-tracker,代碼行數:21,代碼來源:logutil.py

示例2: localize_input

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def localize_input(value, default=None):
    """
    Checks if an input value is a localizable type and returns it
    formatted with the appropriate formatting string of the current locale.
    """
    if isinstance(value, (decimal.Decimal, float) + six.integer_types):
        return number_format(value)
    elif isinstance(value, datetime.datetime):
        value = datetime_safe.new_datetime(value)
        format = force_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
        return value.strftime(format)
    elif isinstance(value, datetime.date):
        value = datetime_safe.new_date(value)
        format = force_str(default or get_format('DATE_INPUT_FORMATS')[0])
        return value.strftime(format)
    elif isinstance(value, datetime.time):
        format = force_str(default or get_format('TIME_INPUT_FORMATS')[0])
        return value.strftime(format)
    return value 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:21,代碼來源:formats.py

示例3: get_connection_params

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def get_connection_params(self):
        settings_dict = self.settings_dict
        # None may be used to connect to the default 'postgres' db
        if settings_dict['NAME'] == '':
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured(
                "settings.DATABASES is improperly configured. "
                "Please supply the NAME value.")
        conn_params = {
            'database': settings_dict['NAME'] or 'postgres',
        }
        conn_params.update(settings_dict['OPTIONS'])
        conn_params.pop('isolation_level', None)
        if settings_dict['USER']:
            conn_params['user'] = settings_dict['USER']
        if settings_dict['PASSWORD']:
            conn_params['password'] = force_str(settings_dict['PASSWORD'])
        if settings_dict['HOST']:
            conn_params['host'] = settings_dict['HOST']
        if settings_dict['PORT']:
            conn_params['port'] = settings_dict['PORT']
        return conn_params 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:24,代碼來源:base.py

示例4: get_connection_params

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def get_connection_params(self):
        kwargs = {
            'conv': django_conversions,
            'charset': 'utf8',
        }
        if six.PY2:
            kwargs['use_unicode'] = True
        settings_dict = self.settings_dict
        if settings_dict['USER']:
            kwargs['user'] = settings_dict['USER']
        if settings_dict['NAME']:
            kwargs['db'] = settings_dict['NAME']
        if settings_dict['PASSWORD']:
            kwargs['passwd'] = force_str(settings_dict['PASSWORD'])
        if settings_dict['HOST'].startswith('/'):
            kwargs['unix_socket'] = settings_dict['HOST']
        elif settings_dict['HOST']:
            kwargs['host'] = settings_dict['HOST']
        if settings_dict['PORT']:
            kwargs['port'] = int(settings_dict['PORT'])
        # We need the number of potentially affected rows after an
        # "UPDATE", not the number of changed rows.
        kwargs['client_flag'] = CLIENT.FOUND_ROWS
        kwargs.update(settings_dict['OPTIONS'])
        return kwargs 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:27,代碼來源:base.py

示例5: __str__

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def __str__(self):
        from django.db import models

        if self.obj is None:
            obj = "?"
        elif isinstance(self.obj, models.base.ModelBase):
            # We need to hardcode ModelBase and Field cases because its __str__
            # method doesn't return "applabel.modellabel" and cannot be changed.
            model = self.obj
            app = model._meta.app_label
            obj = '%s.%s' % (app, model._meta.object_name)
        else:
            obj = force_str(self.obj)
        id = "(%s) " % self.id if self.id else ""
        hint = "\n\tHINT: %s" % self.hint if self.hint else ''
        return "%s: %s%s%s" % (obj, id, self.msg, hint) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:18,代碼來源:messages.py

示例6: build_json_resource_obj

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def build_json_resource_obj(cls, fields, resource, resource_instance, resource_name,
                                force_type_resolution=False):
        """
        Builds the resource object (type, id, attributes) and extracts relationships.
        """
        # Determine type from the instance if the underlying model is polymorphic
        if force_type_resolution:
            resource_name = utils.get_resource_type_from_instance(resource_instance)
        resource_data = [
            ('type', resource_name),
            ('id', encoding.force_str(resource_instance.pk) if resource_instance else None),
            ('attributes', cls.extract_attributes(fields, resource)),
        ]
        relationships = cls.extract_relationships(fields, resource, resource_instance)
        if relationships:
            resource_data.append(('relationships', relationships))
        # Add 'self' link if field is present and valid
        if api_settings.URL_FIELD_NAME in resource and \
                isinstance(fields[api_settings.URL_FIELD_NAME], relations.RelatedField):
            resource_data.append(('links', {'self': resource[api_settings.URL_FIELD_NAME]}))
        return OrderedDict(resource_data) 
開發者ID:django-json-api,項目名稱:django-rest-framework-json-api,代碼行數:23,代碼來源:renderers.py

示例7: test_datefilter_filtered

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_filtered(self):
        self.request_factory = RequestFactory()
        modeladmin = RangeModelDTAdmin(RangeModelDT, site)

        request = self.request_factory.get('/', {'created_at__range__gte': self.today,
                                                 'created_at__range__lte': self.tomorrow})
        request.user = self.user

        changelist = self.get_changelist(request, RangeModelDT, modeladmin)

        queryset = changelist.get_queryset(request)

        self.assertEqual(list(queryset), [self.django_book])
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_str(filterspec.title), 'created at')

        choice = select_by(filterspec.choices(changelist))
        self.assertEqual(choice['query_string'], '?')
        self.assertEqual(choice['system_name'], 'created-at') 
開發者ID:silentsokolov,項目名稱:django-admin-rangefilter,代碼行數:21,代碼來源:tests.py

示例8: test_datefilter_with_default

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_with_default(self):
        self.request_factory = RequestFactory()
        modeladmin = RangeModelDTAdmin(RangeModelDT, site)
        modeladmin.get_rangefilter_created_at_default = lambda r: [self.today, self.tomorrow]

        request = self.request_factory.get('/')
        request.user = self.user

        changelist = self.get_changelist(request, RangeModelDT, modeladmin)

        queryset = changelist.get_queryset(request)

        self.assertEqual(list(queryset), [self.djangonaut_book, self.django_book])
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_str(filterspec.title), 'created at')
        self.assertEqual(filterspec.default_gte, self.today)
        self.assertEqual(filterspec.default_lte, self.tomorrow) 
開發者ID:silentsokolov,項目名稱:django-admin-rangefilter,代碼行數:19,代碼來源:tests.py

示例9: test_datefilter_filtered_with_one_params

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_filtered_with_one_params(self):
        self.request_factory = RequestFactory()
        modeladmin = RangeModelDTAdmin(RangeModelDT, site)

        request = self.request_factory.get('/', {'created_at__range__gte': self.today})
        request.user = self.user

        changelist = self.get_changelist(request, RangeModelDT, modeladmin)

        queryset = changelist.get_queryset(request)

        self.assertEqual(list(queryset), [self.django_book])
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_str(filterspec.title), 'created at')

        choice = select_by(filterspec.choices(changelist))
        self.assertEqual(choice['query_string'], '?')
        self.assertEqual(choice['system_name'], 'created-at') 
開發者ID:silentsokolov,項目名稱:django-admin-rangefilter,代碼行數:20,代碼來源:tests.py

示例10: test_datefilter_filtered_datefield

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_filtered_datefield(self):
        self.request_factory = RequestFactory()
        modeladmin = RangeModelDAdmin(RangeModelD, site)

        request = self.request_factory.get('/', {'created_at__range__gte': self.today,
                                                 'created_at__range__lte': self.tomorrow})
        request.user = self.user

        changelist = self.get_changelist(request, RangeModelD, modeladmin)

        queryset = changelist.get_queryset(request)

        self.assertEqual(list(queryset), [self.django_book_date])
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_str(filterspec.title), 'created at')

        choice = select_by(filterspec.choices(changelist))
        self.assertEqual(choice['query_string'], '?')
        self.assertEqual(choice['system_name'], 'created-at') 
開發者ID:silentsokolov,項目名稱:django-admin-rangefilter,代碼行數:21,代碼來源:tests.py

示例11: test_datetimfilter_filtered

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datetimfilter_filtered(self):
        self.request_factory = RequestFactory()
        modeladmin = RangeModelDTTimeAdmin(RangeModelDT, site)

        request = self.request_factory.get('/', {'created_at__range__gte_0': self.today,
                                                 'created_at__range__gte_1': self.min_time,
                                                 'created_at__range__lte_0': self.tomorrow,
                                                 'created_at__range__lte_1': self.max_time})
        request.user = self.user

        changelist = self.get_changelist(request, RangeModelDT, modeladmin)

        queryset = changelist.get_queryset(request)

        self.assertEqual(list(queryset), [self.django_book])
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_str(filterspec.title), 'created at')

        choice = select_by(filterspec.choices(changelist))
        self.assertEqual(choice['query_string'], '?')
        self.assertEqual(choice['system_name'], 'created-at') 
開發者ID:silentsokolov,項目名稱:django-admin-rangefilter,代碼行數:23,代碼來源:tests.py

示例12: test_datetimfilter_with_default

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datetimfilter_with_default(self):
        self.request_factory = RequestFactory()
        modeladmin = RangeModelDTTimeAdmin(RangeModelDT, site)
        modeladmin.get_rangefilter_created_at_default = lambda r: [self.today, self.tomorrow]

        request = self.request_factory.get('/')
        request.user = self.user

        changelist = self.get_changelist(request, RangeModelDT, modeladmin)

        queryset = changelist.get_queryset(request)

        self.assertEqual(list(queryset), [self.djangonaut_book, self.django_book])
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_str(filterspec.title), 'created at')
        self.assertEqual(filterspec.default_gte, self.today)
        self.assertEqual(filterspec.default_lte, self.tomorrow) 
開發者ID:silentsokolov,項目名稱:django-admin-rangefilter,代碼行數:19,代碼來源:tests.py

示例13: _to_xml

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def _to_xml(self, xml, data):
        if isinstance(data, (list, tuple)):
            for item in data:
                xml.startElement(self.item_tag_name, {})
                self._to_xml(xml, item)
                xml.endElement(self.item_tag_name)

        elif isinstance(data, dict):
            for key, value in data.items():
                xml.startElement(key, {})
                self._to_xml(xml, value)
                xml.endElement(key)

        elif data is None:
            # Don't output any value
            pass

        else:
            xml.characters(force_str(data)) 
開發者ID:jpadilla,項目名稱:django-rest-framework-xml,代碼行數:21,代碼來源:renderers.py

示例14: addition

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def addition(request, object):
    """
    Log that an object has been successfully added.
    """
    models.LogEntry.objects.log_action(
        user_id=request.user.pk,
        content_type_id=ContentType.objects.get_for_model(object).pk,
        object_id=object.pk,
        object_repr=force_str(object),
        action_flag=models.ADDITION,
    ) 
開發者ID:GamesDoneQuick,項目名稱:donation-tracker,代碼行數:13,代碼來源:logutil.py

示例15: deletion

# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def deletion(request, object, object_repr=None):
    """
    Log that an object will be deleted.
    """
    models.LogEntry.objects.log_action(
        user_id=request.user.id,
        content_type_id=ContentType.objects.get_for_model(object).pk,
        object_id=object.pk,
        object_repr=object_repr or force_str(object),
        action_flag=models.DELETION,
    ) 
開發者ID:GamesDoneQuick,項目名稱:donation-tracker,代碼行數:13,代碼來源:logutil.py


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