def migrate_models(self):

        models = ScientificModel.objects.all()

        for model in models:
            authors = self._get_people_from_Persons_table(model.author)
            for author in authors:
                author.save(NAR_client)
            owners = self._get_people_from_Persons_table(model.owner)
            for owner in owners:
                owner.save(NAR_client)
            if len(owners) == 0:
                owners = authors[-1:]
            if len(owners) > 1:
                owners = owners[
                    0]  # temporary, need to fix schema to remove maxCount: 1
            organization = self.get_organization(model.organization)
            brain_region = self.get_parameters("brain_region",
                                               model.brain_region)
            species = self.get_parameters("species", model.species)
            cell_type = self.get_parameters("cell_type", model.cell_type)
            abstraction_level = self.get_parameters("abstraction_level",
                                                    model.abstraction_level)
            model_scope = self.get_parameters("model_scope", model.model_scope)

            model_project = ModelProject(
                name=model.name,
                owners=owners,
                authors=authors,
                description=model.description,
                date_created=model.creation_date,
                private=model.private,
                collab_id=model.app.collab_id,
                alias=model.alias,
                organization=organization,
                pla_components=model.pla_components,
                brain_region=brain_region,
                species=species,
                celltype=cell_type,
                abstraction_level=abstraction_level,
                model_of=model_scope,  # to fix
                images=[{
                    "url": im.url,
                    "caption": im.caption
                } for im in model.images.all()],
                old_uuid=str(model.id))

            #print ("--authors",model_project.authors)
            #print ("--organization",model_project.organization)
            #print ("--brain_region",model_project.brain_region)
            #print ("--species",model_project.species)

            try:
                model_project.save(NAR_client)
            except Exception as err:
                if "internal server error" in err.response.text:
                    logger.error(err)
                else:
                    raise
            else:
                logger.info("ModelProject saved: %s", model_project)
                print(model_project)

        # models_with_parents = ScientificModel.objects.filter(parents__isnull=False)
        # for model in models_with_parents:
        #     mp = ModelProject.by_name(model.name, NAR_client)
        #     for parent_obj in model.parents.all():
        #         parent_kg = ModelProject.by_name(parent_obj.name, NAR_client)
        #         mp.parents = as_list(mp.parents) + [parent_kg]
        #     mp.save(NAR_client)
        return ''
class ScientificModelKGSerializer(BaseKGSerializer):
    def is_valid(self):
        # check alias is unique
        if "alias" in self.data and self.data["alias"]:
            if self.obj and self.obj.alias == self.data["alias"]:
                return True
            logger.debug("Checking for model with same alias")
            model_with_same_alias = ModelProject.from_alias(self.data["alias"],
                                                            self.client,
                                                            api="nexus")
            if bool(model_with_same_alias):
                self.errors.append(
                    "Another model with this alias already exists.")
                return False
        if "private" in self.data:
            if not isinstance(self.data["private"], bool):
                self.errors.append("'private' must be a boolean")
                return False
        if "author" not in self.data or not self.data["author"]:
            self.errors.append("This field may not be blank.")
            return False
        return True  # todo

    def save(self, allow_update=True):
        if self.obj is None:  # create
            for key in ("author", "owner"):
                if isinstance(self.data[key], dict):
                    self.data[key] = [self.data[key]]
            self.obj = ModelProject(
                self.data["name"],
                [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["owner"])
                ],
                [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["author"])
                ],  # need to update person representation in clients,
                self.data.get("description"),
                datetime.now(),
                self.data.get("private", True),
                self.context["collab_id"],
                self.data.get("alias"),
                Organization(self.data["organization"]) if self.data.get(
                    "organization", False) else None,
                pla_components=None,
                brain_region=self._get_ontology_obj(BrainRegion,
                                                    "brain_region"),
                species=self._get_ontology_obj(Species, "species"),
                celltype=self._get_ontology_obj(CellType, "cell_type"),
                abstraction_level=self._get_ontology_obj(
                    AbstractionLevel, "abstraction_level"),
                model_of=self._get_ontology_obj(ModelScope, "model_scope"),
                old_uuid=self.data.get("old_uuid"),
                images=self.data.get("images"))
        else:  # update
            if "name" in self.data:
                self.obj.name = self.data["name"]
            if "alias" in self.data:
                self.obj.alias = self.data["alias"]
            if "author" in self.data:
                self.obj.authors = [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["author"])
                ]  # need to update person representation in clients
            if "owner" in self.data:
                self.obj.owners = [
                    Person(p["family_name"], p["given_name"],
                           p.get("email", None))
                    for p in as_list(self.data["owner"])
                ]  # need to update person representation in clients
            if "app" in self.data:
                self.obj.collab_id = self.data["app"]["collab_id"]
            if "organization" in self.data and self.data[
                    "organization"] is not None:
                self.obj.organization = Organization(self.data["organization"])
            if "private" in self.data:
                self.obj.private = self.data["private"]
            if "cell_type" in self.data:
                self.obj.celltype = self._get_ontology_obj(
                    CellType, "cell_type")
            if "model_scope" in self.data:
                self.obj.model_of = self._get_ontology_obj(
                    ModelScope, "model_scope")
            if "abstraction_level" in self.data:
                self.obj.abstraction_level = self._get_ontology_obj(
                    AbstractionLevel, "abstraction_level")
            if "brain_region" in self.data:
                self.obj.brain_region = self._get_ontology_obj(
                    BrainRegion, "brain_region")
            if "species" in self.data:
                self.obj.species = self._get_ontology_obj(Species, "species")
            if "description" in self.data:
                self.obj.description = self.data["description"]
            if "old_uuid" in self.data:
                self.obj.old_uuid = self.data["old_uuid"]
            if "images" in self.data:
                self.obj.images = self.data["images"]

        # now save people, organization, model. No easy way to make this atomic, I don't think.
        for person in chain(as_list(self.obj.authors),
                            as_list(self.obj.owners)):
            if not isinstance(person, KGProxy):
                # no need to save if we have a proxy object, as
                # that means the person hasn't been updated
                person.save(self.client)
        if self.obj.organization and not isinstance(self.obj.organization,
                                                    KGProxy):
            self.obj.organization.save(self.client)
        self.obj.save(self.client)
        return self.obj

    def serialize(self, model):
        # todo: rewrite all this using KG Query API, to avoid doing all the individual resolves.
        def serialize_person(p):
            if isinstance(p, KGProxy):
                pr = p.resolve(self.client, api="nexus")
            else:
                pr = p
            return {"given_name": pr.given_name, "family_name": pr.family_name}

        data = {
            'id':
            model.uuid,  # extract uuid from uri?
            'uri':
            model.id,
            'name':
            model.name,
            'alias':
            model.alias,
            'author': [serialize_person(au) for au in as_list(model.authors)],
            'owner': [serialize_person(ow) for ow in as_list(model.owners)],
            'app': {
                'collab_id': model.collab_id
            },
            'organization':
            model.organization.resolve(self.client, api="nexus").name
            if model.organization else None,
            'private':
            model.private,
            'cell_type':
            model.celltype.label if model.celltype else None,
            'model_scope':
            model.model_of.label if model.model_of else None,
            'abstraction_level':
            model.abstraction_level.label if model.abstraction_level else None,
            'brain_region':
            model.brain_region.label if model.brain_region else None,
            'species':
            model.species.label if model.species else None,
            'description':
            model.description,
            'images':
            model.images,
            'old_uuid':
            model.old_uuid,
            'instances': [],
            #"raw_data": model.instance.data
        }
        for instance in as_list(model.instances):
            instance = instance.resolve(self.client, api="nexus")
            if not instance:  # if we have a stale reference to a deprecated instance
                continue
            main_script = instance.main_script.resolve(self.client,
                                                       api="nexus")
            instance_data = {
                "id":
                instance.uuid,
                "uri":
                instance.id,
                #"old_uuid": instance.old_uuid
                "version":
                instance.version,
                "description":
                instance.description or '',
                "parameters":
                instance.parameters or '',
                "code_format":
                '',
                "source":
                '',
                "license":
                '',
                "hash":
                '',
                "timestamp":
                instance.timestamp.isoformat() if instance.timestamp else None
            }
            if main_script:
                instance_data.update({
                    "code_format": main_script.code_format or '',
                    "source": main_script.code_location or '',
                    "license": main_script.license or '',
                })
            if hasattr(instance, "morphology"):
                morph = instance.morphology.resolve(self.client, api="nexus")
                instance_data["morphology"] = morph.morphology_file
            data['instances'].append(instance_data)
        return data