Models
Models are the core building blocks of your admin panel. They define the structure of your data and how it is displayed and managed within the admin interface. This guide will walk you through the process of registering a model with go-advanced-admin, configuring its options, and customizing its behavior.
Introduction
In go-advanced-admin, a model represents a data structure that you want to manage through the admin interface. By registering your application's models, you enable CRUD (Create, Read, Update, Delete) operations, list views, and detail views for your data.
Prerequisites
Before proceeding, ensure you have:
A Go project set up with go-advanced-admin installed.
A web framework (e.g., Gin, Echo) and an ORM (e.g., GORM, XORM) integrated.
Basic understanding of Go structs and interfaces.
If you're new to go-advanced-admin, we recommend starting with the Quick Start Guide for your specific web framework and ORM.
Registering a Model
Step 1: Define Your Model
First, define the Go struct that represents your data model. For example, let's create a simple Post
model:
Ensure your model includes all necessary fields and tags required by your ORM.
Step 2: Register the Model with the Admin Panel
To register the Post
model with the admin panel, you need to:
Register an application (if not already done).
Register the model within the application.
Here's how you can do it:
Explanation:
Initialize Gin and GORM: Set up your web framework and ORM.
Create Adapters: Use the provided adapters (
gormadapter
,ginadapter
) to integrate with go-advanced-admin.Permission Function: Define how permissions are handled. In this example, we allow all actions.
Initialize the Admin Panel: Create a new admin panel instance.
Register an Application: Organize your models under an application (e.g., "Blog Management").
Register the Model: Add your
Post
model to the admin panel.
Model Configuration Options
When registering a model, you can customize its behavior and appearance in the admin panel.
Fields
You can control how each field in your model is handled by the admin panel using struct tags.
Example:
Supported Struct Tags:
listDisplay
: Control whether the field is displayed in the list view (include
orexclude
).search
: Control whether the field is searchable (include
orexclude
).addForm
: Control whether the field appears in the "Add" form (include
orexclude
).editForm
: Control whether the field appears in the "Edit" form (include
orexclude
).displayName
: Provide a custom display name for the field.
Display Options
Customize how the model and its fields are displayed.
Model Display Name: Implement the
AdminModelDisplayNameInterface
interface to set a custom display name for the model.func (p *Post) AdminDisplayName() string { return "Blog Post" }Field Display Name: Use the
displayName
struct tag as shown above.
Actions
You can define custom actions or override default behaviors by implementing certain interfaces.
Custom Form Fields: Implement the
AdminFormFieldInterface
to customize form fields.func (p *Post) AdminFormField(name string, isEdit bool) form.Field { if name == "Content" { // Return a custom form field for the Content field } return nil }
Advanced Model Customization
For more advanced use cases, you can:
Implement Custom ORM Logic: Provide a custom ORM integrator if you have special database interactions.
Override Handlers: Customize how requests are handled by providing your own handlers.
Refer to the Advanced Topics section for more details.
References
Quick Start Guide: For a step-by-step setup, check out the Quick Start Guide.
Forms Framework: Learn more about form fields and validation in the Forms section.
Permissions: Understand how to secure your models in the Permissions guide.
Applications: Organize your models using Applications.
Contributing
We welcome contributions to improve the go-advanced-admin project. If you have suggestions or enhancements related to models:
Contribution Guide: Review our Contribution Guide for guidelines.
Issue Tracker: Report issues or feature requests on our
Next Steps:
Explore how to Customize Templates for your models.
Learn about Logging to track changes to your models.
Dive into Integrations for ORM and web framework specifics.
Feedback
If you have questions or need further assistance, feel free to open an issue or join our community discussions.
Thank you for using go-advanced-admin!