uAdmin the Golang Web Framework #2 Managing Your Models

Abdullah Alrasheed
4 min readNov 4, 2018

--

In part one of this tutorial, I cheated to make my blog post shorter by putting my model inside main.go which is not a good idea. Think about how would your project look like when you have 50 models and business logic all jammed inside main.go with over 3K lines of code in one file. To make this scalable, we could add our models inside the models folder and each model should be a separate file. Let’s fix that by adding a file named todo.go under models:

todo/
models/
todo.go
main.go

Now in this file we will put the following code:

package modelsimport (
"github.com/uadmin/uadmin"
"time"
)
type Todo struct {
uadmin.Model
Name string
Description string `uadmin:"html"`
TargetDate time.Time
Progress int `uadmin:"progress_bar"`
}

Now we can edit out main.go and remove the model from there and add a reference to the new todo model.

package mainimport (
"github.com/uadmin/uadmin"
"github.com/your_name/todo/models"
)
func main() {
uadmin.Register(models.Todo{})
uadmin.SiteName = "Todo App"
uadmin.StartServer()
}

Notice that we have to import the models package and refer to our model using models.Todo. Also we did some customization to make the app ours by changing the variable uadmin.SiteName which controls the website name on uAdmin’s UI.

Now you can run your project again:

$ go build; todo
[ OK ] Initializing DB: [9/9]
[ OK ] Server Started: http://0.0.0.0:8080
___ __ _
__ __/ | ____/ /___ ___ (_)___
/ / / / /| |/ __ / __ '__ \/ / __ \
/ /_/ / ___ / /_/ / / / / / / / / / /
\__,_/_/ |_\__,_/_/ /_/ /_/_/_/ /_/

Notice the name on your dashboard and in the title in your browser. We will get to more customization in following parts of this tutorial but for now lets do one more thing, let’s add one more model to our project. The new model will be a larger scope model called task which will have a list of todo. Think of task like a project and todo is a specific thing that you have to do in that project. So each todo has a task and a task could have many todo items (in technical terms one-to-many relationship). The way we do that is, create the new model as a new file called task.go in models folder:

todo/
models/
task.go
todo.go
main.go

In the new file, add the following code:

package modelsimport (
"github.com/uadmin/uadmin"
)
type Task struct {
uadmin.Model
Name string `uadmin:"required"`
TotalProgress int `uadmin:"progress_bar"`
}

Notice that we added a tag called required in the Name field. that will make it required and stop users from saving tasks with no name.

Now let’s edit our todo model and add a foreign key to task. Don’t let the technical terms like “foreign key” scare you, it is just a link from one model to another model (you will get it in a second). Now edit todo.go like this:

package modelsimport (
"github.com/uadmin/uadmin"
"time"
)
type Todo struct {
uadmin.Model
Name string
Description string `uadmin:"html"`
TargetDate time.Time
Progress int `uadmin:"progress_bar"`
Task Task
TaskID uint
}

That’s it! that is how you add a foreign key. Add two fields to your struct, one of the type of the other model and one using the same name followed by ID and type uint.

Now you have to register your new model to uAdmin. To do that, edit your main.go to look like this:

package mainimport (
"github.com/uadmin/uadmin"
"github.com/your_name/todo/models"
)
func main() {
uadmin.Register(
models.Todo{},
models.Task{},
)
uadmin.SiteName = "Todo App"
uadmin.StartServer()
}

now let’s run your project and see what we have.

Notice that we have a new model in the dashboard called “Tasks”. Let’s add a task.

Now you can link a todo to your task.

Now notice that you can see it in the list view after you save

Congrats, you know now how to manage your models and scale your project to a large scale project using models and how to use foreign keys.

In the next part we will talk about inlines, business logic and more customization using tags.

--

--