本文整理汇总了Python中django.db.models.fields.reverse_related.ForeignObjectRel方法的典型用法代码示例。如果您正苦于以下问题:Python reverse_related.ForeignObjectRel方法的具体用法?Python reverse_related.ForeignObjectRel怎么用?Python reverse_related.ForeignObjectRel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.fields.reverse_related
的用法示例。
在下文中一共展示了reverse_related.ForeignObjectRel方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_field_value
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def _get_field_value(self, grpc_field, name, instance):
if instance is None:
return None
# Check if child serializer has special handling method
method = getattr(self, "get_" + name, None)
if method:
return method(instance)
# Default behaviour
field_value = getattr(instance, name)
field_meta = instance._meta.get_field(name)
if isinstance(field_meta, ForeignObjectRel):
return self._serialize_model_relations(grpc_field, field_value.all(), self.serializers)
elif isinstance(field_meta, ForeignKey):
return self.serialize_model(
self.get_grpc_message_class(grpc_field),
field_value,
self.serializers
)
else:
return field_value
示例2: get_field_type_from_lookup
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def get_field_type_from_lookup(ctx: MethodContext, django_context: DjangoContext, model_cls: Type[Model],
*, method: str, lookup: str) -> Optional[MypyType]:
try:
lookup_field = django_context.resolve_lookup_into_field(model_cls, lookup)
except FieldError as exc:
ctx.api.fail(exc.args[0], ctx.context)
return None
except LookupsAreUnsupported:
return AnyType(TypeOfAny.explicit)
if ((isinstance(lookup_field, RelatedField) and lookup_field.column == lookup)
or isinstance(lookup_field, ForeignObjectRel)):
related_model_cls = django_context.get_field_related_model_cls(lookup_field)
if related_model_cls is None:
return AnyType(TypeOfAny.from_error)
lookup_field = django_context.get_primary_key_field(related_model_cls)
field_get_type = django_context.get_field_get_type(helpers.get_typechecker_api(ctx),
lookup_field, method=method)
return field_get_type
示例3: get_field_lookup_exact_type
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def get_field_lookup_exact_type(self, api: TypeChecker, field: Union[Field, ForeignObjectRel]) -> MypyType:
if isinstance(field, (RelatedField, ForeignObjectRel)):
related_model_cls = field.related_model
primary_key_field = self.get_primary_key_field(related_model_cls)
primary_key_type = self.get_field_get_type(api, primary_key_field, method='init')
rel_model_info = helpers.lookup_class_typeinfo(api, related_model_cls)
if rel_model_info is None:
return AnyType(TypeOfAny.explicit)
model_and_primary_key_type = UnionType.make_union([Instance(rel_model_info, []), primary_key_type])
return helpers.make_optional(model_and_primary_key_type)
field_info = helpers.lookup_class_typeinfo(api, field.__class__)
if field_info is None:
return AnyType(TypeOfAny.explicit)
return helpers.get_private_descriptor_type(field_info, '_pyi_lookup_exact_type',
is_nullable=field.null)
示例4: get_field_set_type
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def get_field_set_type(self, api: TypeChecker, field: Union[Field, ForeignObjectRel], *, method: str) -> MypyType:
""" Get a type of __set__ for this specific Django field. """
target_field = field
if isinstance(field, ForeignKey):
target_field = field.target_field
field_info = helpers.lookup_class_typeinfo(api, target_field.__class__)
if field_info is None:
return AnyType(TypeOfAny.from_error)
field_set_type = helpers.get_private_descriptor_type(field_info, '_pyi_private_set_type',
is_nullable=self.get_field_nullability(field, method))
if isinstance(target_field, ArrayField):
argument_field_type = self.get_field_set_type(api, target_field.base_field, method=method)
field_set_type = helpers.convert_any_to_type(field_set_type, argument_field_type)
return field_set_type
示例5: get_field_related_model_cls
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def get_field_related_model_cls(self, field: Union[RelatedField, ForeignObjectRel]) -> Optional[Type[Model]]:
if isinstance(field, RelatedField):
related_model_cls = field.remote_field.model
else:
related_model_cls = field.field.model
if isinstance(related_model_cls, str):
if related_model_cls == 'self':
# same model
related_model_cls = field.model
elif '.' not in related_model_cls:
# same file model
related_model_fullname = field.model.__module__ + '.' + related_model_cls
related_model_cls = self.get_model_class_by_fullname(related_model_fullname)
else:
related_model_cls = self.apps_registry.get_model(related_model_cls)
return related_model_cls
示例6: _resolve_field_from_parts
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def _resolve_field_from_parts(self,
field_parts: Iterable[str],
model_cls: Type[Model]
) -> Union[Field, ForeignObjectRel]:
currently_observed_model = model_cls
field = None
for field_part in field_parts:
if field_part == 'pk':
field = self.get_primary_key_field(currently_observed_model)
continue
field = currently_observed_model._meta.get_field(field_part)
if isinstance(field, RelatedField):
currently_observed_model = field.related_model
model_name = currently_observed_model._meta.model_name
if (model_name is not None
and field_part == (model_name + '_id')):
field = self.get_primary_key_field(currently_observed_model)
if isinstance(field, ForeignObjectRel):
currently_observed_model = field.related_model
assert field is not None
return field
示例7: test_model_fields
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def test_model_fields(self):
field = Contact._meta.get_field("customer")
self.assertIsInstance(field, CompositeForeignKey)
fields = Contact._meta.get_fields()
self.assertIn("customer", [field.name for field in fields])
field2 = Customer._meta.get_field("contacts")
self.assertIsInstance(field2, ForeignObjectRel)
fields2 = Customer._meta.get_fields()
self.assertIn("contacts", [field.name for field in fields2])
self.assertIsNone(field.db_type(None))
示例8: get_model_relations
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def get_model_relations(self, model_cls: Type[Model]) -> Iterator[ForeignObjectRel]:
for field in model_cls._meta.get_fields():
if isinstance(field, ForeignObjectRel):
yield field
示例9: get_field_nullability
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def get_field_nullability(self, field: Union[Field, ForeignObjectRel], method: Optional[str]) -> bool:
nullable = field.null
if not nullable and isinstance(field, CharField) and field.blank:
return True
if method == '__init__':
if ((isinstance(field, Field) and field.primary_key)
or isinstance(field, ForeignKey)):
return True
if method == 'create':
if isinstance(field, AutoField):
return True
if isinstance(field, Field) and field.has_default():
return True
return nullable
示例10: resolve_lookup_into_field
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def resolve_lookup_into_field(self, model_cls: Type[Model], lookup: str) -> Union[Field, ForeignObjectRel]:
query = Query(model_cls)
lookup_parts, field_parts, is_expression = query.solve_lookup_type(lookup)
if lookup_parts:
raise LookupsAreUnsupported()
return self._resolve_field_from_parts(field_parts, model_cls)
示例11: get_field_lookup_exact_type
# 需要导入模块: from django.db.models.fields import reverse_related [as 别名]
# 或者: from django.db.models.fields.reverse_related import ForeignObjectRel [as 别名]
def get_field_lookup_exact_type(api: TypeChecker, field: Field) -> MypyType:
if isinstance(field, (RelatedField, ForeignObjectRel)):
lookup_type_class = field.related_model
rel_model_info = lookup_class_typeinfo(api, lookup_type_class)
if rel_model_info is None:
return AnyType(TypeOfAny.from_error)
return make_optional(Instance(rel_model_info, []))
field_info = lookup_class_typeinfo(api, field.__class__)
if field_info is None:
return AnyType(TypeOfAny.explicit)
return get_private_descriptor_type(field_info, '_pyi_lookup_exact_type',
is_nullable=field.null)