Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions syncano/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,9 @@ def to_python(self, value):
if isinstance(value, dict) and 'type' in value and 'value' in value:
value = value['value']

if isinstance(value, dict) and ('_add' in value or '_remove' in value):
return value

if not isinstance(value, (list, tuple)):
return [value]

Expand All @@ -827,7 +830,7 @@ def to_query(self, value, lookup_type, related_field_name=None, related_field_lo
query_dict = {}

if lookup_type == 'contains':
if self._validate(value):
if self._check_relation_value(value):
value = [obj.id for obj in value]
query_dict = value

Expand All @@ -840,10 +843,13 @@ def to_native(self, value):
if not value:
return None

if isinstance(value, dict) and ('_add' in value or '_remove' in value):
return value

if not isinstance(value, (list, tuple)):
value = [value]

if self._validate(value):
if self._check_relation_value(value):
value = [obj.id for obj in value]
return value

Expand Down
23 changes: 16 additions & 7 deletions syncano/models/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ class RelationValidatorMixin(object):

def validate(self, value, model_instance):
super(RelationValidatorMixin, self).validate(value, model_instance)
self._validate(value)
self._check_relation_value(value)

@classmethod
def _validate(cls, value):
value = cls._make_list(value)
all_ints = all([isinstance(x, int) for x in value])
def _check_relation_value(cls, value):
if value is None:
return False

if '_add' in value or '_remove' in value:
check_value = value.get('_add') or value.get('_remove')
else:
check_value = value

check_value = cls._make_list(check_value)

all_ints = all([isinstance(x, int) for x in check_value])
from .archetypes import Model
all_objects = all([isinstance(obj, Model) for obj in value])
object_types = [type(obj) for obj in value]
all_objects = all([isinstance(obj, Model) for obj in check_value])
object_types = [type(obj) for obj in check_value]
if len(set(object_types)) != 1:
raise SyncanoValueError("All objects should be the same type.")

Expand Down Expand Up @@ -47,7 +56,7 @@ def remove(self, *args):
self._add_or_remove(args, operation='_remove')

def _add_or_remove(self, id_list, operation='_add'):
if self._validate(id_list):
if self._check_relation_value(id_list):
value_ids = [obj.id for obj in id_list]
else:
value_ids = id_list
Expand Down