本文整理匯總了Python中django.db.models.ManyToOneRel方法的典型用法代碼示例。如果您正苦於以下問題:Python models.ManyToOneRel方法的具體用法?Python models.ManyToOneRel怎麽用?Python models.ManyToOneRel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.models
的用法示例。
在下文中一共展示了models.ManyToOneRel方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_new_field_html
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def _get_new_field_html(self, field_name):
try:
f, attr, value = lookup_field(field_name, self.org_obj, self)
except (AttributeError, ObjectDoesNotExist):
return EMPTY_CHANGELIST_VALUE
else:
allow_tags = False
if f is None:
allow_tags = getattr(attr, 'allow_tags', False)
boolean = getattr(attr, 'boolean', False)
if boolean:
allow_tags = True
text = boolean_icon(value)
else:
text = smart_text(value)
else:
if isinstance(f.rel, models.ManyToOneRel):
field_val = getattr(self.org_obj, f.name)
if field_val is None:
text = EMPTY_CHANGELIST_VALUE
else:
text = field_val
else:
text = display_for_field(value, f)
return mark_safe(text) if allow_tags else conditional_escape(text)
示例2: get_field_instance
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [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)
示例3: _get_new_field_html
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def _get_new_field_html(self, field_name):
try:
f, attr, value = lookup_field(field_name, self.org_obj, self)
except (AttributeError, ObjectDoesNotExist):
return EMPTY_CHANGELIST_VALUE
else:
allow_tags = False
if f is None:
allow_tags = getattr(attr, 'allow_tags', False)
boolean = getattr(attr, 'boolean', False)
if boolean:
allow_tags = True
text = boolean_icon(value)
else:
text = smart_unicode(value)
else:
if isinstance(f.rel, models.ManyToOneRel):
field_val = getattr(self.org_obj, f.name)
if field_val is None:
text = EMPTY_CHANGELIST_VALUE
else:
text = field_val
else:
text = display_for_field(value, f)
return mark_safe(text) if allow_tags else conditional_escape(text)
示例4: get_list_queryset
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def get_list_queryset(self):
"""
Get model queryset. The query has been filted and ordered.
"""
# First, get queryset from base class.
queryset = self.queryset()
# Use select_related() if one of the list_display options is a field
# with a relationship and the provided queryset doesn't already have
# select_related defined.
if not queryset.query.select_related:
if self.list_select_related:
queryset = queryset.select_related()
elif self.list_select_related is None:
related_fields = []
for field_name in self.list_display:
try:
field = self.opts.get_field(field_name)
except models.FieldDoesNotExist:
pass
else:
if isinstance(field.rel, models.ManyToOneRel):
related_fields.append(field_name)
if related_fields:
queryset = queryset.select_related(*related_fields)
else:
pass
# Then, set queryset ordering.
queryset = queryset.order_by(*self.get_ordering())
# Return the queryset.
return queryset
# List ordering
示例5: init
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def init(self):
self.label = label_for_field(self.field_name, self.obj.__class__,
model_admin=self.admin_view,
return_attr=False
)
try:
f, attr, value = lookup_field(
self.field_name, self.obj, self.admin_view)
except (AttributeError, ObjectDoesNotExist):
self.text
else:
if f is None:
self.allow_tags = getattr(attr, 'allow_tags', False)
boolean = getattr(attr, 'boolean', False)
if boolean:
self.allow_tags = True
self.text = boolean_icon(value)
else:
self.text = smart_text(value)
else:
if isinstance(f.rel, models.ManyToOneRel):
self.text = getattr(self.obj, f.name)
else:
self.text = display_for_field(value, f)
self.field = f
self.attr = attr
self.value = value
示例6: has_related_field_in_list_display
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def has_related_field_in_list_display(self):
for field_name in self.list_display:
try:
field = self.lookup_opts.get_field(field_name)
except FieldDoesNotExist:
pass
else:
if hasattr(field, 'rel') and isinstance(field.rel, models.ManyToOneRel):
return True
return False
示例7: has_related_field_in_list_display
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def has_related_field_in_list_display(self):
for field_name in self.list_display:
try:
field = self.lookup_opts.get_field(field_name)
except FieldDoesNotExist:
pass
else:
if isinstance(field.remote_field, models.ManyToOneRel):
# <FK>_id field names don't require a join.
if field_name == field.get_attname():
continue
return True
return False
示例8: get_related_models
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def get_related_models(self, model):
fields = model._meta.get_fields(include_hidden=True)
def filter_fields(x):
if any(map(lambda rel: isinstance(x, rel), [
models.OneToOneRel,
models.OneToOneField,
models.ManyToOneRel,
models.ManyToManyField,
models.ManyToManyRel
])):
return True
return False
return list(map(lambda x: x.related_model, filter(filter_fields, fields)))
示例9: get_model_fields
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def get_model_fields(self, model):
fields = model._meta.get_fields()
def filter_fields(x):
if any(map(lambda rel: isinstance(x, rel), [
models.ManyToOneRel,
models.ManyToManyField,
models.ManyToManyRel,
GenericRel,
GenericForeignKey,
GenericRelation
])):
return False
return True
return filter(filter_fields, fields)
示例10: get_list_queryset
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def get_list_queryset(self):
"""
Get model queryset. The query has been filted and ordered.
"""
# First, get queryset from base class.
queryset = self.queryset()
# Use select_related() if one of the list_display options is a field
# with a relationship and the provided queryset doesn't already have
# select_related defined.
if not queryset.query.select_related:
if self.list_select_related:
queryset = queryset.select_related()
elif self.list_select_related is None:
related_fields = []
for field_name in self.list_display:
try:
field = self.opts.get_field(field_name)
except models.FieldDoesNotExist:
pass
else:
if isinstance(field.remote_field, models.ManyToOneRel):
related_fields.append(field_name)
if related_fields:
queryset = queryset.select_related(*related_fields)
else:
pass
# Then, set queryset ordering.
queryset = queryset.order_by(*self.get_ordering())
# Return the queryset.
return queryset
# List ordering
示例11: init
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def init(self):
self.label = label_for_field(self.field_name, self.obj.__class__,
model_admin=self.admin_view,
return_attr=False
)
try:
f, attr, value = lookup_field(
self.field_name, self.obj, self.admin_view)
except (AttributeError, ObjectDoesNotExist):
self.text
else:
if f is None:
self.allow_tags = getattr(attr, 'allow_tags', False)
boolean = getattr(attr, 'boolean', False)
if boolean:
self.allow_tags = True
self.text = boolean_icon(value)
else:
self.text = smart_text(value)
else:
if isinstance(f.remote_field, models.ManyToOneRel):
self.text = getattr(self.obj, f.name)
else:
self.text = display_for_field(value, f)
self.field = f
self.attr = attr
self.value = value
示例12: has_related_field_in_list_display
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def has_related_field_in_list_display(self):
for field_name in self.list_display:
try:
field = self.lookup_opts.get_field(field_name)
except FieldDoesNotExist:
pass
else:
if isinstance(field.remote_field, models.ManyToOneRel):
# <FK>_id field names don't require a join.
if field_name != field.get_attname():
return True
return False
示例13: has_related_field_in_list_display
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def has_related_field_in_list_display(self):
for field_name in self.list_display:
try:
field = self.opts.get_field(field_name)
except FieldDoesNotExist:
pass
else:
if isinstance(field, models.ManyToOneRel):
return True
return False
示例14: setup_db_compat_django
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import ManyToOneRel [as 別名]
def setup_db_compat_django():
from tri_table import register_column_factory
try:
# noinspection PyUnresolvedReferences
from django.db.models import IntegerField, FloatField, TextField, BooleanField, AutoField, CharField, DateField, DateTimeField, DecimalField, EmailField, TimeField, ForeignKey, ManyToOneRel, ManyToManyField, ManyToManyRel, UUIDField
except ImportError:
pass
else:
# The order here is significant because of inheritance structure. More specific must be below less specific.
register_column_factory(CharField, Shortcut(call_target__attribute='text'))
register_column_factory(UUIDField, Shortcut(call_target__attribute='text'))
register_column_factory(TimeField, Shortcut(call_target__attribute='time'))
register_column_factory(EmailField, Shortcut(call_target__attribute='email'))
register_column_factory(DecimalField, Shortcut(call_target__attribute='decimal'))
register_column_factory(DateField, Shortcut(call_target__attribute='date'))
register_column_factory(DateTimeField, Shortcut(call_target__attribute='datetime'))
register_column_factory(BooleanField, Shortcut(call_target__attribute='boolean'))
register_column_factory(TextField, Shortcut(call_target__attribute='text'))
register_column_factory(FloatField, Shortcut(call_target__attribute='float'))
register_column_factory(IntegerField, Shortcut(call_target__attribute='integer'))
register_column_factory(AutoField, Shortcut(call_target__attribute='integer', show=False))
register_column_factory(ManyToOneRel, None)
register_column_factory(ManyToManyField, Shortcut(call_target__attribute='many_to_many'))
register_column_factory(ManyToManyRel, None)
register_column_factory(ForeignKey, Shortcut(call_target__attribute='foreign_key'))