Skip to content

M2M setup in admin #1

@AyliD

Description

@AyliD

Hi,

im using a simple code to test M2m Fields , code is pasted below.

Every time i try to add new sub and save it i get this error:

no such column: sub.blogs
no such column: sub.blogs
table sub has no column named blogs

Code:

package main

import (
"net/http"
"strings"
"time"

"github.com/astaxie/beego/orm"
_ "github.com/mattn/go-sqlite3"
"github.com/oal/admin"

)

type Category struct {
Id int orm:"auto"
Title string admin:"list search"
Description string admin:"list blank null default='No description.'" orm:"null"
}

type Sub struct {
Id int orm:"auto"
Title string admin:"list search"
Description string admin:"list blank null default='No description.'" orm:"null"
Blogs []*Blog admin:"list='Id' label='M2M' blank null width=2" orm:"reverse(many);rel_through(main.Blogsubs)"
}

func (c *Category) SortBy() string {
return "Title"
}

type BlogPost struct {
Id int orm:"auto"
Category *Category admin:"list='Id' label='Category' width=2" orm:"rel(fk)" // list='Title' is used to show Category.Title instead of Category.Id in list view.
Title string admin:"list search width=7"
Photo string admin:"width=3 field='file' upload_to='static/posts'" // File field
Body string admin:"textarea" orm:"type(text)"
Published time.Time admin:"list width=11"
Draft bool admin:"list width=1"
}

type Blog struct {
Id int orm:"auto"
Subs []*Sub admin:"list='Id' label='Sub' blank null width=2" orm:"rel(m2m);rel_through(main.Blogsubs)" // list='Title' is used to show Category.Title instead of Category.Id in list view.
Title string admin:"list search width=7"
//Photo string admin:"width=3 field='file' upload_to='static/posts'" // File field
Body string admin:"textarea" orm:"type(text)"
//Published time.Time admin:"list width=11"
Draft bool admin:"list width=1"
}

func (b *BlogPost) AdminName() string {
return "Blog post"
}

type Blogsubs struct {
Id int64
Blog *Blog orm:"rel(fk)"
Sub *Sub orm:"rel(fk)"
}

func (self *Blogsubs) TableName() string {
return "blog__subs"
}

func main() {
// Beego related
orm.RegisterDataBase("default", "sqlite3", "db.sqlite")
orm.RegisterModel(new(Category))
orm.RegisterModel(new(Sub))
orm.RegisterModel(new(BlogPost))
orm.RegisterModel(new(Blog))
orm.RegisterModel(new(Blogsubs))
orm.RunCommand()

// Set up admin
a, err := admin.New("/admin", "sqlite3", "db.sqlite")
if err != nil {
    panic(err)
}

// Override settings as needed
a.Title = "Example admin"
a.NameTransform = snakeString // Optional, but needed here to be compatible with Beego ORM
a.User("admin", "example")    // Username / password to log in.

group, err := a.Group("Blog")
if err != nil {
    panic(err)
}
group.RegisterModel(new(Category))
group.RegisterModel(new(Sub))
group.RegisterModel(new(BlogPost))
group.RegisterModel(new(Blog))
group.RegisterModel(new(Blogsubs))
// Get a http.Handler to attach to your router/mux.
adminHandler, err := a.Handler()
if err != nil {
    panic(err)
}

// Serve admin.
router := http.NewServeMux()
router.Handle("/admin/", adminHandler)
router.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
    rw.Write([]byte("Nothing to see here. Visit /admin/ instead."))
})

http.ListenAndServe(":8002", router)

}

// snakeString converts struct fields from CamelCase to snake_case for Beego ORM
func snakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '')
}
if d != '
' {
j = true
}
data = append(data, d)
}
return strings.ToLower(string(data[:len(data)]))
}


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions