本文整理匯總了Python中django.contrib.gis.db.models.ForeignKey方法的典型用法代碼示例。如果您正苦於以下問題:Python models.ForeignKey方法的具體用法?Python models.ForeignKey怎麽用?Python models.ForeignKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.gis.db.models
的用法示例。
在下文中一共展示了models.ForeignKey方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_related_fields
# 需要導入模塊: from django.contrib.gis.db import models [as 別名]
# 或者: from django.contrib.gis.db.models import ForeignKey [as 別名]
def create_related_fields(self):
"""
Create ForeignKey and ManyFields for each self.configuration.related_fields
:return:
"""
return map_dict_to_dict(
lambda field_name, related_field_configuration: self.create_related_field(
field_name,
related_field_configuration),
self.configuration.related_fields or {})
示例2: dynamic_join_model_class
# 需要導入模塊: from django.contrib.gis.db import models [as 別名]
# 或者: from django.contrib.gis.db.models import ForeignKey [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
示例3: create_related_field
# 需要導入模塊: from django.contrib.gis.db import models [as 別名]
# 或者: from django.contrib.gis.db.models import ForeignKey [as 別名]
def create_related_field(self, field_name, related_field_configuration):
"""
Creates a ForeignKey or ManyToMany field based on related_field_configuration
:param field_name:
:param related_field_configuration: A dict containing related_key or related_class_name
related_key: the instance of the sibling key of the config_entity whose dynamic_model_class is the relation type.
For DbEntity/Features this would be the db_entity_key and the dynamic_model_class would be its FeatureClass
For AnalysisModule this would be the analysis module key and its dynamic class
related_class_name: rather than related_key, any model class, such as BuiltForm, to relate to.
single: if True this is a ForeignKey (toOne) relationship. Otherwise a ManyToMany is created
:return: A two-item tuple. First item is the field name and the second is the field.
"""
config_entity = ConfigEntity._subclassed_by_id(self.configuration.scope)
# TODO temp coverage of a key name name change
related_field_configuration['related_key'] = related_field_configuration.get('related_key', related_field_configuration.get('related_db_entity_key'))
if related_field_configuration.get('related_key', False):
# field name matches name of peer self.db_entity_key--get it's feature class name
related_db_entity = get_single_value_or_none(config_entity.computed_db_entities(key=related_field_configuration['related_key']))
# Always call with no_ensure=True since the config_entity is the same. Otherwise we'd get infinite recursion
related_class_name_or_model = self.__class__(self.config_entity, related_db_entity, no_ensure=True).dynamic_model_class()
elif related_field_configuration.get('related_class_name', None):
# A model class such as BuiltForm
related_class_name_or_model = resolve_module_attr(related_field_configuration['related_class_name'])
else:
raise Exception("No related_key or related_class_name found on feature_class_configuration for self.configuration.key %s" % self.configuration.key)
logger.info('Creating %r related field for %s using %s', related_field_configuration.get('single', None) and 'single' or 'm2m',
field_name, related_field_configuration)
if related_field_configuration.get('single', None):
return [field_name,
models.ForeignKey(related_class_name_or_model, null=True)]
else:
return [field_name,
models.ManyToManyField(related_class_name_or_model,
# Pass a custom, readable table name for the through class for ManyToMany relations
db_table='"{schema}"."{table}_{field_name}"'.format(
schema=config_entity.schema(),
table=self.configuration.key,
field_name=field_name))]