From: Scott Baker Date: Thu, 18 Dec 2014 20:59:04 +0000 (-0800) Subject: finished support for ManyToMany with through relations in REST API X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=2fdc6383309af237a82c21759faeb954dfdd3d90;p=plstackapi.git finished support for ManyToMany with through relations in REST API --- diff --git a/planetstack/apigen/api.template.py b/planetstack/apigen/api.template.py index 5302c62..b1b0251 100644 --- a/planetstack/apigen/api.template.py +++ b/planetstack/apigen/api.template.py @@ -70,11 +70,29 @@ class XOSModelSerializer(serializers.ModelSerializer): (relatedObject, data) = stuff through = relatedObject.field.rel.through local_fieldName = relatedObject.field.m2m_reverse_field_name() - print "XXX", accessor, relatedObject, data + remote_fieldName = relatedObject.field.m2m_field_name() + + # get the current set of existing relations existing = through.objects.filter(**{local_fieldName: obj}); - print "existing", existing + data_ids = [item.id for item in data] + existing_ids = [getattr(item,remote_fieldName).id for item in existing] + + #print "data_ids", data_ids + #print "existing_ids", existing_ids + + # remove relations that are in 'existing' but not in 'data' + for item in list(existing): + if (getattr(item,remote_fieldName).id not in data_ids): + print "delete", getattr(item,remote_fieldName) + item.delete() #(purge=True) + # add relations that are in 'data' but not in 'existing' + for item in data: + if (item.id not in existing_ids): + #print "add", item + newModel = through(**{local_fieldName: obj, remote_fieldName: item}) + newModel.save() {% for object in generator.all %}