本文整理匯總了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