本文整理汇总了Python中tastypie.fields.ToManyField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.ToManyField方法的具体用法?Python fields.ToManyField怎么用?Python fields.ToManyField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tastypie.fields
的用法示例。
在下文中一共展示了fields.ToManyField方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dynamic_join_model_class
# 需要导入模块: from tastypie import fields [as 别名]
# 或者: from tastypie.fields import ToManyField [as 别名]
def dynamic_join_model_class(self, join_models, related_field_names):
"""
Creates an unmanaged subclass of the feature class with extra fields to represent the
the fields of the join_models. This also adds fields for any fields specified in the
related_model_lookup. This is not for join models but ForeignKeys such as BuiltForm
These latter fields must be specified explicitly because the main model and join models
can't populate their foreign keys from the query because the query has to be
a ValuesQuerySet in order to do the join. So we create id versions of the fields here
(e.g. built_form_id) which the query can fill and then use that to manually
set the foreign key reference in the Tastypie resource.
:param join_models: Other feature models whose attributes should be added to the subclass
:param related_field_names: List of field names of foreign key id fields (AutoFields)
"""
main_model_class = self.dynamic_model_class()
manager = main_model_class.objects
# Exclude the following field types. Since the base Feature defines an id we'll still get that, which we want
exclude_field_types = (ForeignKey, ToManyField, OneToOneField, GeometryField)
all_field_paths_to_fields = merge(
# Create fields to represented foreign key id fields
# Our query fetches these ids since it can't fetch related objects (since its a values() query)
map_to_dict(
lambda field_name: [field_name.replace('__', '_x_'),
IntegerField(field_name.replace('__', '_x_'), null=True)],
related_field_names
),
# The join fields for each joined related model
*map(
lambda related_model: related_field_paths_to_fields(
manager,
related_model,
exclude_field_types=exclude_field_types,
fields=limited_api_fields(related_model),
separator='_x_'),
join_models)
)
abstract_feature_class = resolve_module_attr(self.configuration.abstract_class_name)
# Make sure the class name is unique to the related models and the given ConfigEntity
related_models_unique_id = '_'.join(sorted(map(lambda related_model: related_model.__name__, join_models), ))
dynamic_model_clazz = dynamic_model_class(
main_model_class,
self.db_entity.schema,
self.db_entity.table,
class_name="{0}{1}{2}{3}Join".format(
abstract_feature_class.__name__,
self.db_entity.id,
self.config_entity.id,
related_models_unique_id),
fields=all_field_paths_to_fields,
class_attrs=self.configuration.class_attrs or {},
related_class_lookup=self.configuration.related_class_lookup or {},
is_managed=False,
cacheable=False)
logger.info("Created dynamic join model class %s" % dynamic_model_clazz)
logger.debug("Created with model fields %s" % map(lambda field: field.name, dynamic_model_clazz._meta.fields))
logger.debug("Created with related and join fields %s" % all_field_paths_to_fields)
return dynamic_model_clazz