本文整理匯總了Python中taggit.managers.TaggableManager方法的典型用法代碼示例。如果您正苦於以下問題:Python managers.TaggableManager方法的具體用法?Python managers.TaggableManager怎麽用?Python managers.TaggableManager使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類taggit.managers
的用法示例。
在下文中一共展示了managers.TaggableManager方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: to_dict
# 需要導入模塊: from taggit import managers [as 別名]
# 或者: from taggit.managers import TaggableManager [as 別名]
def to_dict(self):
data = {}
# Iterate over croncrete and many-to-many fields
for field in self._meta.concrete_fields + self._meta.many_to_many:
value = None
# If the value of the field is another model, fetch an instance of it
# Exception made of tags for which we just retrieve the list of them for later
# conversion to simple strings
if isinstance(field, ForeignKey):
value = self.__getattribute__(field.name)
elif isinstance(field, ManyToManyField):
value = list(self.__getattribute__(field.name).all())
elif isinstance(field, TaggableManager):
value = list(self.__getattribute__(field.name).all())
else:
value = self.__getattribute__(field.name)
# If the instance of a model as a to_dict() function, call it
if isinstance(value, TemplateModel):
data[field.name] = value.to_dict()
elif isinstance(value, list):
data[field.name] = []
for element in value:
if isinstance(element, TemplateModel):
data[field.name].append(element.to_dict())
else:
data[field.name].append(str(element))
else:
data[field.name] = value
return data
示例2: __get__
# 需要導入模塊: from taggit import managers [as 別名]
# 或者: from taggit.managers import TaggableManager [as 別名]
def __get__(self, instance, model):
# override TaggableManager's requirement for instance to have a primary key
# before we can access its tags
manager = _ClusterTaggableManager(
through=self.through, model=model, instance=instance, prefetch_cache_name=self.name
)
return manager
示例3: get_comparison_class
# 需要導入模塊: from taggit import managers [as 別名]
# 或者: from taggit.managers import TaggableManager [as 別名]
def get_comparison_class(self):
# Hide fields with hidden widget
widget_override = self.widget_overrides().get(self.field_name, None)
if widget_override and widget_override.is_hidden:
return
try:
field = self.db_field
if field.choices:
return compare.ChoiceFieldComparison
if field.is_relation:
if isinstance(field, TaggableManager):
return compare.TagsFieldComparison
elif field.many_to_many:
return compare.M2MFieldComparison
return compare.ForeignObjectComparison
if isinstance(field, RichTextField):
return compare.RichTextFieldComparison
if isinstance(field, (CharField, TextField)):
return compare.TextFieldComparison
except FieldDoesNotExist:
pass
return compare.FieldComparison
示例4: filter_queryset
# 需要導入模塊: from taggit import managers [as 別名]
# 或者: from taggit.managers import TaggableManager [as 別名]
def filter_queryset(self, request, queryset, view):
"""
This performs field level filtering on the result set
Eg: ?title=James Joyce
"""
fields = set(view.get_available_fields(queryset.model, db_fields_only=True))
for field_name, value in request.GET.items():
if field_name in fields:
try:
field = queryset.model._meta.get_field(field_name)
except LookupError:
field = None
# Convert value into python
try:
if isinstance(field, (models.BooleanField, models.NullBooleanField)):
value = parse_boolean(value)
elif isinstance(field, (models.IntegerField, models.AutoField)):
value = int(value)
except ValueError as e:
raise BadRequestError("field filter error. '%s' is not a valid value for %s (%s)" % (
value,
field_name,
str(e)
))
if isinstance(field, TaggableManager):
for tag in value.split(','):
queryset = queryset.filter(**{field_name + '__name': tag})
# Stick a message on the queryset to indicate that tag filtering has been performed
# This will let the do_search method know that it must raise an error as searching
# and tag filtering at the same time is not supported
queryset._filtered_by_tag = True
else:
queryset = queryset.filter(**{field_name: value})
return queryset
示例5: get_value
# 需要導入模塊: from taggit import managers [as 別名]
# 或者: from taggit.managers import TaggableManager [as 別名]
def get_value(self, obj):
from taggit.managers import TaggableManager
try:
field = self.get_field(obj.__class__)
value = field.value_from_object(obj)
if hasattr(field, 'get_searchable_content'):
value = field.get_searchable_content(value)
elif isinstance(field, TaggableManager):
# As of django-taggit 1.0, value_from_object returns a list of Tag objects,
# which matches what we want
pass
elif isinstance(field, RelatedField):
# The type of the ForeignKey may have a get_searchable_content method that we should
# call. Firstly we need to find the field its referencing but it may be referencing
# another RelatedField (eg an FK to page_ptr_id) so we need to run this in a while
# loop to find the actual remote field.
remote_field = field
while isinstance(remote_field, RelatedField):
remote_field = remote_field.target_field
if hasattr(remote_field, 'get_searchable_content'):
value = remote_field.get_searchable_content(value)
return value
except FieldDoesNotExist:
value = getattr(obj, self.field_name, None)
if hasattr(value, '__call__'):
value = value()
return value
示例6: get_model_variables
# 需要導入模塊: from taggit import managers [as 別名]
# 或者: from taggit.managers import TaggableManager [as 別名]
def get_model_variables(model):
variables = []
for field in model._meta.concrete_fields + model._meta.many_to_many:
variable = None
if isinstance(field, ForeignKey):
variable = "`{}`: {} object".format(
field.name, get_model_file_link(field.related_model)
)
elif isinstance(field, ManyToManyField):
variable = "`{}`: list of {} objects".format(
field.name, get_model_file_link(field.related_model)
)
elif isinstance(field, TaggableManager):
variable = "`{}`: list of tags".format(field.name)
else:
field_type = None
if isinstance(field, BooleanField):
field_type = "boolean"
elif isinstance(field, CharField):
field_type = "string"
elif isinstance(field, TextField):
field_type = "text"
elif isinstance(field, DateTimeField):
field_type = "date"
elif isinstance(field, InetAddressField):
field_type = "IP address"
elif (
isinstance(field, AutoField)
or isinstance(field, BigIntegerField)
or isinstance(field, PositiveIntegerField)
or isinstance(field, PositiveSmallIntegerField)
):
field_type = "integer".format(field.name)
else:
print(
"Unknown type for `{}`: {}".format(
field.name, field.get_internal_type()
)
)
if field_type:
variable = "`{}`: {} value".format(field.name, field_type)
if variable:
variables.append(variable)
return variables