def insert_child(obj, child_model, full_clean=False, **attrs):
    """Create and save an instance of `child_model` from existing `obj`.

    If `full_clean` is True, call full_clean on the newly created
    object. Default is `False` because this was the historic
    behaviour.

    """
    #~ assert child_model != obj.__class__
    #~ if child_model == obj.__class__:
        #~ raise ValidationError(
            #~ "A %s cannot be parent for itself" %
            #~ obj.__class__.__name__)
    parent_link_field = child_model._meta.parents.get(obj.__class__, None)
    if parent_link_field is None:
        raise ValidationError(str("A %s cannot be parent for a %s" % (
            obj.__class__.__name__, child_model.__name__)))
    attrs[parent_link_field.name] = obj
    # ~ for pm,pf in child_model._meta.parents.items(): # pm : parent model, pf : parent link field
        #~ attrs[pf.name] = obj
    #~ attrs["%s_ptr" % obj.__class__.__name__.lower()] = obj
    if AFTER17:
        fields_list = obj._meta.concrete_fields
    else:
        fields_list = obj._meta.fields
    for field in fields_list:
        attrs[field.name] = getattr(obj, field.name)
    new_obj = child_model(**attrs)
    #~ logger.info("20120830 insert_child %s",obj2str(new_obj))

    new_obj.save()
    # on_add_child.send(sender=obj, child=new_obj)
    if full_clean:
        try:
            new_obj.full_clean()
            new_obj.save()
        except __HOLE__ as e:
            msg = obj.error2str(e)
            raise ValidationError(
                _("Problem while inserting %(child)s "
                  "child of %(parent)s: %(message)s") %
                dict(child=child_model.__name__,
                     parent=obj.__class__.__name__,
                     message=msg))
    return new_obj
        

#~ def insert_child_and_save(obj,child_model,**attrs):
    #~ """
    #~ Insert (create) and save a `child_model` instance of existing `obj`.
    #~ """
    #~ obj = insert_child(obj,child_model,**attrs)
    #~ obj.save()
    #~ return obj
dataset/ETHPy150Open lsaffre/lino/lino/utils/mti.py/insert_child