def __init__(self, to_model, base_field=None, size=None, related_name=None, symmetrical=None,
related_query_name=None, limit_choices_to=None, to_field=None, db_constraint=False, **kwargs):
try:
to = to_model._meta.model_name
except AttributeError:
assert isinstance(to_model, six.string_types), (
"%s(%r) is invalid. First parameter to ForeignKey must be "
"either a model, a model name, or the string %r" % (
self.__class__.__name__, to_model,
RECURSIVE_RELATIONSHIP_CONSTANT,
)
)
to = str(to_model)
else:
# For backwards compatibility purposes, we need to *try* and set
# the to_field during FK construction. It won't be guaranteed to
# be correct until contribute_to_class is called. Refs #12190.
to_field = to_field or (to_model._meta.pk and to_model._meta.pk.name)
if not base_field:
field = to_model._meta.get_field(to_field)
if not field.is_relation:
base_field_type = type(field)
internal_type = field.get_internal_type()
if internal_type == 'AutoField':
pass
elif internal_type == 'BigAutoField':
base_field = models.BigIntegerField()
elif hasattr(field, 'max_length'):
base_field = base_field_type(max_length = field.max_length)
else:
base_field = base_field_type()
if not base_field:
base_field = models.IntegerField()
if symmetrical is None:
symmetrical = (to == RECURSIVE_RELATIONSHIP_CONSTANT)
kwargs['rel'] = self.rel_class(
self, to, to_field,
related_name=related_name,
related_query_name=related_query_name,
limit_choices_to=limit_choices_to,
symmetrical=symmetrical,
)
self.has_null_arg = 'null' in kwargs
self.db_constraint = db_constraint
self.to = to
if 'default' not in kwargs.keys():
kwargs['default'] = []
kwargs['blank'] = True
self.from_fields = ['self']
self.to_fields = [to_field]
super(ArrayManyToManyField, self).__init__(base_field, size=size, **kwargs)
评论列表
文章目录