本文整理匯總了Python中django.contrib.contenttypes.fields.GenericForeignKey方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.GenericForeignKey方法的具體用法?Python fields.GenericForeignKey怎麽用?Python fields.GenericForeignKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.contenttypes.fields
的用法示例。
在下文中一共展示了fields.GenericForeignKey方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _details
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def _details(self, current_model, relation):
direct = isinstance(relation, Field) or isinstance(relation, GenericForeignKey)
model = relation.model._meta.concrete_model
if model == current_model:
model = None
field = relation if direct else relation.field
return relation, model, direct, bool(field.many_to_many) # many_to_many can be None
示例2: get_related_fields
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def get_related_fields(model):
return {
field.name: field
for field in model._meta.get_fields()
if field.is_relation and not isinstance(field, (GenericForeignKey, GenericRel))
}
示例3: test_order_with_respect_to_private_field
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def test_order_with_respect_to_private_field(self):
class PrivateFieldModel(models.Model):
content_type = models.ForeignKey('contenttypes.ContentType', models.CASCADE)
object_id = models.PositiveIntegerField()
private = GenericForeignKey()
class Meta:
order_with_respect_to = 'private'
state = ModelState.from_model(PrivateFieldModel)
self.assertNotIn('order_with_respect_to', state.options)
示例4: test_generic_fk
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def test_generic_fk(self):
A = self.create_model("A", foreign_keys=[
models.ForeignKey('B', models.CASCADE),
GenericForeignKey(),
])
B = self.create_model("B", foreign_keys=[
models.ForeignKey('C', models.CASCADE),
])
self.assertRelated(A, [B])
self.assertRelated(B, [A])
示例5: _getattr_for_field_tracking
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def _getattr_for_field_tracking(self, attr):
"""Avoid N+1 for foreignkeys, store there id instead of value"""
if isinstance(getattr(self.__class__, attr), GenericForeignKey): # return the instance for ContentType attributes. Caution, this does N+1
return getattr(self, attr, None)
# Try to return ID instead of instance
return getattr(self, f'{attr}_id', None) or getattr(self, attr, None)
示例6: test_str
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def test_str(self):
class Model(models.Model):
field = GenericForeignKey()
self.assertEqual(str(Model.field), 'contenttypes_tests.Model.field')
示例7: _get_generic_fields
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def _get_generic_fields():
"""
Return a list of all GenericForeignKeys in all models.
"""
generic_fields = []
for model in apps.get_models():
for field_name, field in model.__dict__.items():
if isinstance(field, GenericForeignKey):
generic_fields.append(field)
return generic_fields
示例8: restore_object
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def restore_object(self, attrs, instance=None):
"""
Restore the model instance.
"""
m2m_data = {}
related_data = {}
nested_forward_relations = {}
# noinspection PyProtectedMember
meta = self.opts.model._meta
# Reverse fk or one-to-one relations
for (obj, model) in meta.get_all_related_objects_with_model():
field_name = obj.get_accessor_name()
if field_name in attrs:
related_data[field_name] = attrs.pop(field_name)
# Reverse m2m relations
for (obj, model) in meta.get_all_related_m2m_objects_with_model():
field_name = obj.get_accessor_name()
if field_name in attrs:
m2m_data[field_name] = attrs.pop(field_name)
# Forward m2m relations
if issubclass(meta.many_to_many.__class__, tuple):
temp_m2m = list(meta.many_to_many)
else:
temp_m2m = meta.many_to_many
for field in temp_m2m + meta.virtual_fields:
if isinstance(field, GenericForeignKey):
continue
if field.name in attrs:
m2m_data[field.name] = attrs.pop(field.name)
# Nested forward relations - These need to be marked so we can save
# them before saving the parent model instance.
for field_name in attrs.keys():
if isinstance(self.fields.get(field_name, None), Serializer):
nested_forward_relations[field_name] = attrs[field_name]
# Create an empty instance of the model
if instance is None:
instance = self.opts.model()
for key, val in attrs.items():
try:
setattr(instance, key, val)
except ValueError:
self._errors[key] = [self.error_messages['required']]
# Any relations that cannot be set until we've
# saved the model get hidden away on these
# private attributes, so we can deal with them
# at the point of save.
instance._related_data = related_data
instance._m2m_data = m2m_data
instance._nested_forward_relations = nested_forward_relations
return instance
示例9: get_expected_types
# 需要導入模塊: from django.contrib.contenttypes import fields [as 別名]
# 或者: from django.contrib.contenttypes.fields import GenericForeignKey [as 別名]
def get_expected_types(self, api: TypeChecker, model_cls: Type[Model], *, method: str) -> Dict[str, MypyType]:
from django.contrib.contenttypes.fields import GenericForeignKey
expected_types = {}
# add pk if not abstract=True
if not model_cls._meta.abstract:
primary_key_field = self.get_primary_key_field(model_cls)
field_set_type = self.get_field_set_type(api, primary_key_field, method=method)
expected_types['pk'] = field_set_type
for field in model_cls._meta.get_fields():
if isinstance(field, Field):
field_name = field.attname
field_set_type = self.get_field_set_type(api, field, method=method)
expected_types[field_name] = field_set_type
if isinstance(field, ForeignKey):
field_name = field.name
foreign_key_info = helpers.lookup_class_typeinfo(api, field.__class__)
if foreign_key_info is None:
# maybe there's no type annotation for the field
expected_types[field_name] = AnyType(TypeOfAny.unannotated)
continue
related_model = self.get_field_related_model_cls(field)
if related_model is None:
expected_types[field_name] = AnyType(TypeOfAny.from_error)
continue
if related_model._meta.proxy_for_model is not None:
related_model = related_model._meta.proxy_for_model
related_model_info = helpers.lookup_class_typeinfo(api, related_model)
if related_model_info is None:
expected_types[field_name] = AnyType(TypeOfAny.unannotated)
continue
is_nullable = self.get_field_nullability(field, method)
foreign_key_set_type = helpers.get_private_descriptor_type(foreign_key_info,
'_pyi_private_set_type',
is_nullable=is_nullable)
model_set_type = helpers.convert_any_to_type(foreign_key_set_type,
Instance(related_model_info, []))
expected_types[field_name] = model_set_type
elif isinstance(field, GenericForeignKey):
# it's generic, so cannot set specific model
field_name = field.name
gfk_info = helpers.lookup_class_typeinfo(api, field.__class__)
gfk_set_type = helpers.get_private_descriptor_type(gfk_info, '_pyi_private_set_type',
is_nullable=True)
expected_types[field_name] = gfk_set_type
return expected_types