Skip to content

Metadata API

Metadata is data that describes other data. To understand how Grit One SDK defines metadata, contrast business data with Grit One metadata. Business data includes the records that directly correspond to your company’s business such as an address, account, or product. Grit One metadata describes the schema, process, presentation, authorization, and general configuration of your deployed platform.

Examples

from grit.core.metadata import metadata
from grit.core.db.models.task import Task
from grit.sales.models import Lead, Account, Contact, Opportunity


class ContactInline(admin.TabularInline):
    model = Contact
    extra = 0
    verbose_name = "Contact"
    verbose_name_plural = "Contacts"
    fields = ('first_name', 'last_name', 'email')


class OpportunityInline(admin.TabularInline):
    model = Opportunity
    extra = 0
    verbose_name = "Opportunity"
    verbose_name_plural = "Opportunities"
    fields = ('name', 'stage', 'amount', 'close_date')


@metadata.register(Lead)
class LeadMetadata(metadata.ModelMetadata):
    list_display = ('name', 'email', 'message', 'created_at')
    list_actions = [
        [{
            'label': 'New Lead',
            'action': 'new'
        }]
    ]
    list_bulk_actions = [
        {'label': 'Delete', 'action': 'delete'}
    ]
    fieldsets = (
        ('Basic Information', {
            'fields': ('name','email', 'message', 'created_at')
        }),
    )


@metadata.register(Account)
class AccountMetadata(metadata.ModelMetadata):
    list_display = ('name', 'owner')
    list_views = {
        'all': {
            'label': 'All Accounts',
            'fields': ('name', 'owner'),
            'filters': {}
        },
    }
    list_actions = [
        [{
            'label': 'New Account',
            'action': 'new'
        }]
    ]
    fieldsets = (
        ('Basic Information', {
            'fields': ('name',)
        }),
    )
    inlines = [ContactInline, OpportunityInline]


@metadata.register(Contact)
class ContactMetadata(metadata.ModelMetadata):
    list_display = ('first_name', 'last_name', 'email', 'owner')
    list_actions = [
        [{
            'label': 'New Contact',
            'action': 'new'
        }]
    ]
    fieldsets = (
        ('Basic Information', {
            'fields': ('first_name', 'last_name', 'email', 'phone', 'account')
        }),
    )


class TaskInline(admin.TabularInline):
    model = Task
    extra = 0
    verbose_name = "Task"
    verbose_name_plural = "Tasks"
    fields = ('title', 'status', 'due_datetime', 'owner')


@metadata.register(Task)
class TaskMetadata(metadata.ModelMetadata):
    fieldsets = (
        ('Basic Information', {
            'fields': ('title', 'description', 'status', 'due_datetime', 'what')
        }),
    )


@metadata.register(Opportunity)
class OpportunityMetadata(metadata.ModelMetadata):
    list_display = ('name', 'account', 'stage', 'amount', 'close_date')
    list_actions = [
        [{
            'label': 'New Opportunity',
            'action': 'new'
        }]
    ]
    list_views = {
        'all': {
            'label': 'All',
            'fields': ('name', 'email', 'message', 'created_at'),
            'filters': {}
        },
        'new': {
            'label': 'New',
            'fields': ('name', 'email', 'created_at'),
            'filters': {
                'stage': 'new'
            }
        }
    }
    fieldsets = (
        ('Basic Information', {
            'fields': ('name', 'account', 'stage', 'amount', 'close_date')
        }),
    )
    inlines = [TaskInline]

Customizing List Views

List views let you define named presets for a model's list page. Each preset can display different columns and apply different filters. Users switch between views using a dropdown on the list page. The active view is controlled by the ?view= query parameter; when omitted, the first defined view is selected by default.

Each view accepts the following options:

Option Type Description
label string Display name shown in the view selector dropdown.
fields tuple Fields to display as columns. Overrides list_display for this view.
filters dict Django ORM filters applied to the queryset (e.g., {'stage': 'new'}).
list_views = {
    'all': {
        'label': 'All',
        'fields': ('name', 'email', 'message', 'created_at'),
        'filters': {}
    },
    'new': {
        'label': 'New',
        'fields': ('name', 'email', 'created_at'),
        'filters': {
            'stage': 'new'
        }
    }
}

List Actions

List actions define the action buttons available on a model's list page. They appear in a grouped dropdown menu. Each group is a list of action dictionaries, and groups are visually separated in the menu.

Each action accepts the following options:

Option Type Description
label string Display text shown on the action button.
action string Action type. Use 'new' to create a record (auto-generates the URL and redirects).
list_actions = [
    [{
        'label': 'New Lead',
        'action': 'new'
    }]
]