uAdmin Tutorial Part 7 - M2M (Many-to-many)¶
uAdmin has a multiselection feature that allows you to select multiple values inside an input box field.
Before we proceed, run your application first. Let’s add more records in the Category model.
First Record
Second Record
Third Record
Result
Exit your application. Let’s add Category field with the array data type of the Category model, set the type tag as “list_exclude”, and add CategoryList field as well with the type tag “read_only” in models/category.go so we can see the result in the list page after you save the record.
Category []Category `uadmin:"list_exclude"`
CategoryList string `uadmin:"read_only"`
Expected result
// Item Model !
type Item struct {
uadmin.Model
Name string `uadmin:"search;categorical_filter;filter;display_name:Product Name"`
Description string `uadmin:"multilingual"`
// FIELDS ADDED
Category []Category `uadmin:"list_exclude" gorm:"many2many:-"`
CategoryList string `uadmin:"read_only"`
Cost int `uadmin:"money;pattern:^[0-9]*$;pattern_msg:Your input must be a number."`
Rating int `uadmin:"min:1;max:5"`
}
Copy this one as well and paste it below the Item struct.
// Save !
func (i *Item) Save() {
// Add a new string array type variable called categoryList
categoryList := []string{}
// Append every element to the categoryList array
for c := range i.Category {
categoryList = append(categoryList, i.Category[c].Name)
}
// Concatenate the categoryList to a single string separated by comma
joinList := strings.Join(categoryList, ", ")
// Store the joined string to the CategoryList field
i.CategoryList = joinList
// Save it to the database
uadmin.Save(i)
}
Now rebuild your application, go to ITEMS model from uAdmin dashboard, and inside it, click Add New Item button on the top right corner. In the Category field, add new tag there.
Result
Well done! Now you know how to create multiple elements in M2M tag field and concatenate into a single string stored in another field to display in the list page.
Click here to view our progress so far.
In the next part, we will discuss on how to apply validation in the back-end.