def is_valid_field(self, model, field):
# Split with maximum splits of 1, so if passed xx__yy__zz, we get [xx, yy__zz]
components = field.split(LOOKUP_SEP, 1)
try:
field = model._meta.get_field(components[0])
# Reverse lookup
if isinstance(field, ForeignObjectRel):
return self.is_valid_field(field.model, components[1])
if field.get_internal_type() in self.related_field_types and len(components) > 1:
return self.is_valid_field(field.related_model, components[1])
return True
except FieldDoesNotExist:
return False
python类ForeignObjectRel()的实例源码
def is_valid_field(self, model, field):
"""
Return true if the field exists within the model (or in the related
model specified using the Django ORM __ notation)
"""
components = field.split('__', 1)
try:
field, parent_model, direct, m2m = \
model._meta.get_field_by_name(components[0])
# reverse relation
if isinstance(field, ForeignObjectRel):
return self.is_valid_field(field.model, components[1])
# foreign key
if field.rel and len(components) == 2:
return self.is_valid_field(field.rel.to, components[1])
return True
except FieldDoesNotExist:
return False
_django_db_models_base.py 文件源码
项目:Tinychat-Bot--Discontinued
作者: Tinychat
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def getCustomProperties(self):
self.fields = {}
self.relations = {}
self.columns = []
self.meta = self.klass._meta
for name in self.meta.get_all_field_names():
x = self.meta.get_field_by_name(name)[0]
if isinstance(x, files.FileField):
self.readonly_attrs.update([name])
if isinstance(x, ForeignObjectRel):
continue
if isinstance(x, models.ManyToManyField):
self.relations[name] = x
elif not isinstance(x, models.ForeignKey):
self.fields[name] = x
else:
self.relations[name] = x
parent_fields = []
for field in self.meta.parents.values():
parent_fields.append(field.attname)
del self.relations[field.name]
self.exclude_attrs.update(parent_fields)
props = self.fields.keys()
self.encodable_properties.update(props)
self.decodable_properties.update(props)
self.exclude_attrs.update(['_state'])
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):
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
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def test_model_fields(self):
f = Contact._meta.get_field("customer")
self.assertIsInstance(f, CompositeForeignKey)
l = Contact._meta.get_fields()
self.assertIn("customer", [field.name for field in l])
f2 = Customer._meta.get_field("contacts")
self.assertIsInstance(f2, ForeignObjectRel)
l2 = Customer._meta.get_fields()
self.assertIn("contacts", [field.name for field in l2])
self.assertIsNone(f.db_type(None))
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
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
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,
}
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
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
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,
}
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
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,
}
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def _django_get_all_field_names(model):
from itertools import chain
from django.db.models import Manager
from django.db.models.fields.related import ForeignObjectRel
return list(set(chain.from_iterable(
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
for field in model._meta.get_fields()
if not isinstance(field, ForeignObjectRel)
)))
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def choices(self, cl):
# #### MPTT ADDITION START
try:
# EMPTY_CHANGELIST_VALUE has been removed in django 1.9
from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
except:
EMPTY_CHANGELIST_VALUE = self.empty_value_display
# #### MPTT ADDITION END
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, padding_style 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,
# #### MPTT ADDITION START
'padding_style': padding_style,
# #### MPTT ADDITION END
}
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,
}
def is_related_field(field):
return isinstance(field,ForeignObjectRel)
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def get_model_fields(self):
"""
Method to get all model fields for the content type
associated with the forms specified content type
:return: List of model field instances
"""
def is_valid_field(field):
if isinstance(field, (models.AutoField, ForeignObjectRel, GenericRelation, GenericForeignKey)):
return False
else:
return True
return list(filter(is_valid_field, self.content_type.model_class()._meta.get_fields()))
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
def is_related_field(field):
return isinstance(field, ForeignObjectRel)
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
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,
}