'Attempt to create or update an item.
Returns the instance if attempt suceeded.
Raises NeedsDependency if a dependency must be created first.'

def load_item(self, item, Model, metadata):

    natural_key_spec = metadata['natural_key']
    relations = metadata.get('relations', {})
    for (name, key) in zip(natural_key_spec, item['key']):
        relation = relations.get(name, {})
        if relation:
            instances = self.instances.get(relation['resource'], {})
            if (key not in instances):
                raise self.NeedsDependency(relation, key)
    for (name, value) in item['relations'].items():
        relation = relations[name]
        if (relation['link'] == 'to_one'):
            required = [value]
        else:
            required = value
        instances = self.instances.get(relation['resource'], {})
        for key in required:
            if (key not in instances):
                raise self.NeedsDependency(relation, key)
    key_dict = {}
    for (name, key) in zip(natural_key_spec, item['key']):
        relation = relations.get(name, {})
        if relation:
            rel = self.instances[relation['resource']][key]
            key_dict[name] = rel
        else:
            key_dict[name] = key
    defaults = item['fields'].copy()
    for (name, value) in item['relations'].items():
        relation = relations[name]
        if (relation['link'] == 'to_one'):
            rel = self.instances[relation['resource']][value]
            defaults[name] = rel
    (instance, created) = Model.objects.get_or_create(defaults=defaults, **key_dict)
    if (not created):
        for (name, value) in item['fields'].items():
            setattr(instance, name, value)
        for (name, value) in item['relations'].items():
            relation = relations[name]
            if (relation['link'] == 'to_one'):
                rel = self.instances[relation['resource']][value]
                setattr(instance, name, rel)
        instance.save()
    for (name, values) in item['relations'].items():
        relation = relations[name]
        if (relation['link'] == 'to_many'):
            for value in values:
                rel = self.instances[relation['resource']][value]
                getattr(instance, name).add(rel)
    return instance
