数据关系如下:

type Student struct{
	Id int 				`form:"-"`
	Name string				`form:"username"`
	Score float64				`form:"score"`
	Class *Class `orm:"rel(fk)"		form:"class"`
	Teacher []*Teacher `orm:"rel(m2m)"	form:"teacher"`
}

type Teacher struct{
	Id int
	Name string
	Class []*Class `orm:"rel(m2m)"`
	Student []*Student `orm:"reverse(many)"`
}

type Class struct{
	Id int
	Name string
	Student []*Student `orm:"reverse(many)"`
	Teacher []*Teacher `orm:"reverse(many)"`
}


普通插入数据:

        classname := this.Input().Get("name")

        fmt.Println(classname)

        var class models.Class

        class.Name = classname

        id, err := orm.NewOrm().Insert(&class)

        if err == nil{

            fmt.Println(id)

        }


fk插入数据:


        classmodel := models.Class{Id:classid}

        student.Class = &classmodel

        id, err := o.Insert(student)

        fmt.Println("Added err: ", err)

        if err == nil{

            fmt.Println("Added id: ", id)

        }


M2M插入数据:      


        o := orm.NewOrm()

        fmt.Println(this.Input())

        teachername := this.Input().Get("name")

         //该处为获取列表数组,需要用GetStrings 而不是GetString

        teacherclass := this.GetStrings("class")

        // 多对多添加测试

        // var classid string

        teacher := &models.Teacher{Name: teachername}

        for _, classId := range teacherclass{

         // 由于Id为int型,而获取的classId为string,需要转换为int,注意记得import:strconv

        classid, _ := strconv.Atoi(classId)

        class := models.Class{Id : classid}

        m2m := o.QueryM2M(&class, "Teacher")

        o.Insert(teacher)

        num, err := m2m.Add(teacher)

        if err == nil {

            fmt.Println("Added nums: ", num)

        }