// view for update object func (this *ArticleAdminRouter) Update() { form := models.ArticleAdminForm{} if this.ValidFormSets(&form) == false { return } // get changed field names changes := utils.FormChanges(&this.object, &form) url := fmt.Sprintf("/admin/article/%d", this.object.Id) // update changed fields only if len(changes) > 0 { form.SetToArticle(&this.object) if err := this.object.Update(changes...); err == nil { this.FlashRedirect(url, 302, "UpdateSuccess") return } else { beego.Error(err) this.Data["Error"] = err } } else { this.Redirect(url, 302) } }
// view for new object save func (this *ArticleAdminRouter) Save() { form := models.ArticleAdminForm{Create: true} if !this.ValidFormSets(&form) { return } var article models.Article form.SetToArticle(&article) if err := article.Insert(); err == nil { this.FlashRedirect(fmt.Sprintf("/admin/article/%d", article.Id), 302, "CreateSuccess") return } else { beego.Error(err) this.Data["Error"] = err } }
// view for edit object func (this *ArticleAdminRouter) Edit() { form := models.ArticleAdminForm{} form.SetFromArticle(&this.object) this.SetFormSets(&form) }