本文整理汇总了Python中django.db.models.ManyToManyRel方法的典型用法代码示例。如果您正苦于以下问题:Python models.ManyToManyRel方法的具体用法?Python models.ManyToManyRel怎么用?Python models.ManyToManyRel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.ManyToManyRel方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def display_for_field(value, field):
from xadmin.views.list import EMPTY_CHANGELIST_VALUE
if field.flatchoices:
return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
# NullBooleanField needs special-case null-handling, so it comes
# before the general null test.
elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
return boolean_icon(value)
elif value is None:
return EMPTY_CHANGELIST_VALUE
elif isinstance(field, models.DateTimeField):
return formats.localize(tz_localtime(value))
elif isinstance(field, (models.DateField, models.TimeField)):
return formats.localize(value)
elif isinstance(field, models.DecimalField):
return formats.number_format(value, field.decimal_places)
elif isinstance(field, models.FloatField):
return formats.number_format(value)
elif isinstance(field.rel, models.ManyToManyRel):
return ', '.join([smart_text(obj) for obj in value.all()])
else:
return smart_text(value)
示例2: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def display_for_field(value, field):
from xadmin.views.list import EMPTY_CHANGELIST_VALUE
if field.flatchoices:
return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
# NullBooleanField needs special-case null-handling, so it comes
# before the general null test.
elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
return boolean_icon(value)
elif value is None:
return EMPTY_CHANGELIST_VALUE
elif isinstance(field, models.DateTimeField):
return formats.localize(tz_localtime(value))
elif isinstance(field, (models.DateField, models.TimeField)):
return formats.localize(value)
elif isinstance(field, models.DecimalField):
return formats.number_format(value, field.decimal_places)
elif isinstance(field, models.FloatField):
return formats.number_format(value)
elif isinstance(field.remote_field, models.ManyToManyRel):
return ', '.join([smart_text(obj) for obj in value.all()])
else:
return smart_text(value)
示例3: get_field_instance
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [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)
示例4: validate_local_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def validate_local_fields(e, opts, field_name, fields):
from django.db import models
if not isinstance(fields, collections.Sequence):
e.add(opts, 'all %s elements must be sequences' % field_name)
else:
for field in fields:
try:
f = opts.get_field(field, many_to_many=True)
except models.FieldDoesNotExist:
e.add(opts, '"%s" refers to %s, a field that doesn\'t exist.' % (field_name, field))
else:
if isinstance(f.rel, models.ManyToManyRel):
e.add(opts, '"%s" refers to %s. ManyToManyFields are not supported in %s.' % (field_name, f.name, field_name))
if f not in opts.local_fields:
e.add(opts, '"%s" refers to %s. This is not in the same model as the %s statement.' % (field_name, f.name, field_name))
示例5: display_for_field
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def display_for_field(value, field):
from xadmin.views.list import EMPTY_CHANGELIST_VALUE
if field.flatchoices:
return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE)
# NullBooleanField needs special-case null-handling, so it comes
# before the general null test.
elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField):
return boolean_icon(value)
elif value is None:
return EMPTY_CHANGELIST_VALUE
elif isinstance(field, models.DateTimeField):
return formats.localize(tz_localtime(value))
elif isinstance(field, (models.DateField, models.TimeField)):
return formats.localize(value)
elif isinstance(field, models.DecimalField):
return formats.number_format(value, field.decimal_places)
elif isinstance(field, models.FloatField):
return formats.number_format(value)
elif isinstance(field.rel, models.ManyToManyRel):
return ', '.join([smart_unicode(obj) for obj in value.all()])
else:
return smart_unicode(value)
示例6: lookup_needs_distinct
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def lookup_needs_distinct(opts, lookup_path):
"""
Returns True if 'distinct()' should be used to query the given lookup path.
"""
field_name = lookup_path.split('__', 1)[0]
field = opts.get_field(field_name)
if ((hasattr(field, 'rel') and
isinstance(field.rel, models.ManyToManyRel)) or
(is_related_field(field) and
not field.field.unique)):
return True
return False
示例7: get_version_object
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def get_version_object(self, version):
obj_version = version._object_version
obj = obj_version.object
obj._state.db = self.obj._state.db
for field_name, pks in obj_version.m2m_data.items():
f = self.opts.get_field(field_name)
if f.rel and isinstance(f.rel, models.ManyToManyRel):
setattr(obj, f.name, f.rel.to._default_manager.get_query_set(
).filter(pk__in=pks).all())
detail = self.get_model_view(DetailAdminUtil, self.model, obj)
return obj, detail
示例8: get_related_models
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [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 ManyToManyRel [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: lookup_needs_distinct
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def lookup_needs_distinct(opts, lookup_path):
"""
Returns True if 'distinct()' should be used to query the given lookup path.
"""
field_name = lookup_path.split('__', 1)[0]
field = opts.get_field(field_name)
if ((hasattr(field, 'remote_field') and
isinstance(field.remote_field, models.ManyToManyRel)) or
(is_related_field(field) and
not field.field.unique)):
return True
return False
示例11: setup_db_compat_django
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [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'))
示例12: get_reverse_fields
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ManyToManyRel [as 别名]
def get_reverse_fields(model, local_field_names):
for name, attr in model.__dict__.items():
# Don't duplicate any local fields
if name in local_field_names:
continue
# "rel" for FK and M2M relations and "related" for O2O Relations
related = getattr(attr, "rel", None) or getattr(attr, "related", None)
if isinstance(related, models.ManyToOneRel):
yield (name, related)
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
yield (name, related)