本文整理汇总了Python中django.db.models.fields.related.ForeignObjectRel方法的典型用法代码示例。如果您正苦于以下问题:Python related.ForeignObjectRel方法的具体用法?Python related.ForeignObjectRel怎么用?Python related.ForeignObjectRel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.fields.related
的用法示例。
在下文中一共展示了related.ForeignObjectRel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_field_instance
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def get_field_instance(self, model, field_name):
field = model._meta.get_field(field_name)
field_kwargs = {'model': model, 'name': field.name}
if field.is_relation:
if not field.related_model:
# GenericForeignKey
return
if self.excluded(field.related_model):
return
field_cls = RelationField
field_kwargs['related_model'] = field.related_model
else:
field_cls = self.get_field_cls(field)
if isinstance(field, (ManyToOneRel, ManyToManyRel, ForeignObjectRel)):
# Django 1.8 doesn't have .null attribute for these fields
field_kwargs['nullable'] = True
else:
field_kwargs['nullable'] = field.null
field_kwargs['suggest_options'] = (
field.name in self.suggest_options.get(model, [])
)
return field_cls(**field_kwargs)
示例2: choices
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def choices(self):
yield {
'selected': self.lookup_exact_val == '' and not self.lookup_isnull_val,
'query_string': self.query_string({},
[self.lookup_exact_name, self.lookup_isnull_name]),
'display': _('All'),
}
for pk_val, val in self.lookup_choices:
yield {
'selected': self.lookup_exact_val == smart_unicode(pk_val),
'query_string': self.query_string({
self.lookup_exact_name: pk_val,
}, [self.lookup_isnull_name]),
'display': val,
}
if (isinstance(self.field, ForeignObjectRel)
and self.field.field.null or hasattr(self.field, 'rel')
and self.field.null):
yield {
'selected': bool(self.lookup_isnull_val),
'query_string': self.query_string({
self.lookup_isnull_name: 'True',
}, [self.lookup_exact_name]),
'display': EMPTY_CHANGELIST_VALUE,
}
示例3: is_related_field
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
示例4: has_output
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def has_output(self):
if (isinstance(self.field, ForeignObjectRel) and
self.field.field.null or hasattr(self.field, 'rel') and
self.field.null):
extra = 1
else:
extra = 0
return len(self.lookup_choices) + extra > 1
示例5: choices
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def choices(self, cl):
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
yield {
'selected': self.lookup_val is None and not self.lookup_val_isnull,
'query_string': cl.get_query_string({},
[self.lookup_kwarg, self.lookup_kwarg_isnull]),
'display': _('All'),
}
for pk_val, val in self.lookup_choices:
yield {
'selected': self.lookup_val == smart_text(pk_val),
'query_string': cl.get_query_string({
self.lookup_kwarg: pk_val,
}, [self.lookup_kwarg_isnull]),
'display': val,
}
if (isinstance(self.field, ForeignObjectRel) and
(self.field.field.null or isinstance(self.field.field, ManyToManyField)) or
hasattr(self.field, 'rel') and (self.field.null or isinstance(self.field, ManyToManyField))):
yield {
'selected': bool(self.lookup_val_isnull),
'query_string': cl.get_query_string({
self.lookup_kwarg_isnull: 'True',
}, [self.lookup_kwarg]),
'display': EMPTY_CHANGELIST_VALUE,
}
示例6: __init__
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def __init__(self, data=None, files=None, instance=None, queryset=None, **kwargs):
if instance is None:
self.instance = self.fk.remote_field.model()
else:
self.instance = instance
self.rel_name = ForeignObjectRel(self.fk, self.fk.remote_field.model, related_name=self.fk.remote_field.related_name).get_accessor_name()
if queryset is None:
queryset = getattr(self.instance, self.rel_name).all()
super(BaseChildFormSet, self).__init__(data, files, queryset=queryset, **kwargs)
示例7: get_value
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def get_value(self, obj):
field = self.get_field(obj.__class__)
if isinstance(field, (RelatedField, ForeignObjectRel)):
return getattr(obj, self.field_name)
示例8: select_on_queryset
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def select_on_queryset(self, queryset):
"""
This method runs either prefetch_related or select_related on the queryset
to improve indexing speed of the relation.
It decides which method to call based on the number of related objects:
- single (eg ForeignKey, OneToOne), it runs select_related
- multiple (eg ManyToMany, reverse ForeignKey) it runs prefetch_related
"""
try:
field = self.get_field(queryset.model)
except FieldDoesNotExist:
return queryset
if isinstance(field, RelatedField) and not isinstance(field, ParentalManyToManyField):
if field.many_to_one or field.one_to_one:
queryset = queryset.select_related(self.field_name)
elif field.one_to_many or field.many_to_many:
queryset = queryset.prefetch_related(self.field_name)
elif isinstance(field, ForeignObjectRel):
# Reverse relation
if isinstance(field, OneToOneRel):
# select_related for reverse OneToOneField
queryset = queryset.select_related(self.field_name)
else:
# prefetch_related for anything else (reverse ForeignKey/ManyToManyField)
queryset = queryset.prefetch_related(self.field_name)
return queryset
示例9: test
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def test(cls, field, request, params, model, admin_view, field_path):
if not (hasattr(field, 'rel') and bool(field.rel) or isinstance(field, ForeignObjectRel)):
return False
related_modeladmin = admin_view.admin_site._registry.get(
get_model_from_relation(field))
return related_modeladmin and getattr(related_modeladmin, 'relfield_style', None) == 'fk-ajax'
示例10: has_output
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def has_output(self):
if (isinstance(self.field, ForeignObjectRel)
and self.field.field.null or hasattr(self.field, 'rel')
and self.field.null):
extra = 1
else:
extra = 0
return len(self.lookup_choices) + extra > 1
示例11: get_field_parts
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def get_field_parts(model, field_name):
"""
Get the field parts that represent the traversable relationships from the
base ``model`` to the final field, described by ``field_name``.
ex::
>>> parts = get_field_parts(Book, 'author__first_name')
>>> [p.verbose_name for p in parts]
['author', 'first name']
"""
parts = field_name.split(LOOKUP_SEP)
opts = model._meta
fields = []
# walk relationships
for name in parts:
try:
field = opts.get_field(name)
except FieldDoesNotExist:
return None
fields.append(field)
if isinstance(field, RelatedField):
opts = remote_model(field)._meta
elif isinstance(field, ForeignObjectRel):
opts = field.related_model._meta
return fields
示例12: verbose_field_name
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def verbose_field_name(model, field_name):
"""
Get the verbose name for a given ``field_name``. The ``field_name``
will be traversed across relationships. Returns '[invalid name]' for
any field name that cannot be traversed.
ex::
>>> verbose_field_name(Article, 'author__name')
'author name'
"""
if field_name is None:
return '[invalid name]'
parts = get_field_parts(model, field_name)
if not parts:
return '[invalid name]'
names = []
for part in parts:
if isinstance(part, ForeignObjectRel):
if part.related_name:
names.append(part.related_name.replace('_', ' '))
else:
return '[invalid name]'
else:
names.append(force_text(part.verbose_name))
return ' '.join(names)
示例13: test_get_related_object
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def test_get_related_object(self):
field_info = self._details(Person, Person._meta.get_field('relating_baseperson'))
self.assertEqual(field_info[1:], (BasePerson, False, False))
self.assertIsInstance(field_info[0], related.ForeignObjectRel)
示例14: test_get_related_m2m
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def test_get_related_m2m(self):
field_info = self._details(Person, Person._meta.get_field('relating_people'))
self.assertEqual(field_info[1:], (None, False, True))
self.assertIsInstance(field_info[0], related.ForeignObjectRel)
示例15: lookup_allowed
# 需要导入模块: from django.db.models.fields import related [as 别名]
# 或者: from django.db.models.fields.related import ForeignObjectRel [as 别名]
def lookup_allowed(self, lookup, value):
# Check FKey lookups that are allowed, so that popups produced by
# ForeignKeyRawIdWidget, on the basis of ForeignKey.limit_choices_to,
# are allowed to work.
for l in self.model._meta.related_fkey_lookups:
for k, v in widgets.url_params_from_lookup_dict(l).items():
if k == lookup and v == value:
return True
parts = lookup.split(LOOKUP_SEP)
# Last term in lookup is a query term (__exact, __startswith etc)
# This term can be ignored.
if len(parts) > 1 and parts[-1] in QUERY_TERMS:
parts.pop()
# Special case -- foo__id__exact and foo__id queries are implied
# if foo has been specifically included in the lookup list; so
# drop __id if it is the last part. However, first we need to find
# the pk attribute name.
rel_name = None
for part in parts[:-1]:
try:
field, _, _, _ = self.model._meta.get_field_by_name(part)
except FieldDoesNotExist:
# Lookups on non-existent fields are ok, since they're ignored
# later.
return True
if hasattr(field, 'rel'):
if field.rel is None:
# This property or relation doesn't exist, but it's allowed
# since it's ignored in ChangeList.get_filters().
return True
model = field.rel.to
rel_name = field.rel.get_related_field().name
elif isinstance(field, ForeignObjectRel):
model = field.model
rel_name = model._meta.pk.name
else:
rel_name = None
if rel_name and len(parts) > 1 and parts[-1] == rel_name:
parts.pop()
if len(parts) == 1:
return True
clean_lookup = LOOKUP_SEP.join(parts)
return clean_lookup in self.list_filter