Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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, [])
)
field_instance = field_cls(**field_kwargs)
# Check if suggested options conflict with field type
if field_cls != StrField and field_instance.suggest_options:
for option in field_instance.get_options():
if isinstance(option, text_type):
closed_set = list(exclude)
while open_set:
model = open_set.popleft()
model_label = self.model_label(model)
if model_label in closed_set:
continue
model_fields = OrderedDict()
for field in self.get_fields(model):
if not isinstance(field, DjangoQLField):
field = self.get_field_instance(model, field)
if not field:
continue
if isinstance(field, RelationField):
if field.relation not in closed_set:
model_fields[field.name] = field
open_set.append(field.related_model)
else:
model_fields[field.name] = field
result[model_label] = model_fields
closed_set.append(model_label)
return result
def __init__(self, model, name, related_model, nullable=False,
suggest_options=False):
super(RelationField, self).__init__(
model=model,
name=name,
nullable=nullable,
suggest_options=suggest_options,
)
self.related_model = related_model
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, [])
)
field_instance = field_cls(**field_kwargs)
# Check if suggested options conflict with field type
if field_cls != StrField and field_instance.suggest_options:
for option in field_instance.get_options():
if isinstance(option, text_type):
# Convert to StrField
field_instance = StrField(**field_kwargs)
return field_instance