Fields

Djing ships with a variety of fields out of the box, including fields for text inputs, booleans, dates, file uploads, Markdown, and more.

Defining Fields

Each Djing resource contains a fields method. This method returns an array of fields, which generally extend the Djing.Fields.Field class. Djing ships with a variety of fields out of the box, including fields for text inputs, booleans, dates, file uploads, Markdown, and more.

To add a field to a resource, you may simply add it to the resource’s fields method. Typically, fields may be created using their static make method. This method accepts several arguments; however, you usually only need to pass the “human readable” name of the field. Djing will automatically “snake case” this string to determine the underlying database column:

djing_admin/app/Djing/User.py
from djing.core.Fields.ID import ID
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            ID.make("ID").sortable(),
            Text.make("Username").sortable()

Field Column Conventions

As noted above, Djing will “snake case” the displayable name of the field to determine the underlying database column. However, if necessary, you may pass the column name as the second argument to the field’s make method:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text

# ...

Text.make('Username', 'username_column'),

Showing / Hiding Fields

Often, you will only want to display a field in certain situations. For example, there is typically no need to show a Password field on a resource index listing. Likewise, you may wish to only display a Created At field on the creation / update forms. Djing makes it a breeze to hide / show fields on certain pages.

The following methods may be used to show / hide fields based on the display context:

  • show_on_index

  • show_on_detail

  • show_on_creating

  • show_on_updating

  • hide_from_index

  • hide_from_detail

  • only_on_index

  • only_on_detail

  • only_on_forms

  • except_on_forms

Dynamic Field Methods

If your application requires it, you may specify a separate list of fields for specific display contexts. For example, imagine you have a resource with the following list of fields:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Firstname").sortable(),
            Text.make("Lastname").sortable()
        ]

On your detail page, you may wish to show a combined name via a computed field, followed by the job title. In order to do this, you could add a fields_for_detail method to the resource class which returns a separate list of fields that should only be displayed on the resource’s detail page:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields_for_detail(self, request: DjingRequest):
        return [
            Text.make("Name", lambda: f"{self.first_name} {self.last_name}"),
        ]

The available methods that may be defined for individual display contexts are:

  • fields_for_index

  • fields_for_detail

  • fields_for_inline_create

  • fields_for_create

  • fields_for_update

Default Values

There are times you may wish to provide a default value to your fields. Djing offers this functionality via the default method, which accepts a value or callback. This value will be used as the field’s default input value on the resource’s creation view:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Name").default(lambda request: "John Doe"),
        ]

Field Placeholder Text

By default, the placeholder text of a field will be it’s name. You can override the placeholder text of a field that supports placeholders by using the placeholder method:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Name").placeholder("John Doe"),
        ]

Field Hydration

On every create or update request that Djing receives for a given resource, each field’s corresponding model attribute will automatically be filled before the model is persisted to the database. If necessary, you may customize the hydration behavior of a given field using the fill_using method:

djing_admin/app/Djing/User.py
from Illuminate.Support.Str import Str
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...
    
    def _fill_using_callback(
        self, request: DjingRequest, request_attribute, model, attribute
    ):
        model[attribute] = Str.title(request.query_param(request_attribute))

    def fields(self, request: DjingRequest):
        return [
            Text.make("Name").fill_using(self._fill_using_callback),
        ]

Sortable Field

When attaching a field to a resource, you may use the sortable method to indicate that the resource index may be sorted by the given field:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Username").sortable(),
        ]

Field Types

Djing ships with a variety of field types. So, let’s explore all of the available types and their options:

Avatar

The Avatar field extends the Image field and accepts the same options and configuration:

djing_admin/app/Djing/User.py
from djing.core.Fields.Avatar import Avatar
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Avatar.make("Avatar"),
        ]

You may use the squared method to display the image’s thumbnail with squared edges. Additionally, you may use the rounded method to display its thumbnails with fully-rounded edges:

djing_admin/app/Djing/User.py
from djing.core.Fields.Avatar import Avatar
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Avatar.make("Avatar").squared(),
        ]

Boolean

The Boolean field may be used to represent a boolean / “tiny integer” column in your database. For example, assuming your database has a boolean column named active, you may attach a Boolean field to your resource like so:

djing_admin/app/Djing/User.py
from djing.core.Fields.Boolean import Boolean
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Boolean.make("Active"),
        ]

— Customizing True / False Values

If you are using values other than true, false, 1, or 0 to represent “true” and “false”, you may instruct Djing to use the custom values recognized by your application. To accomplish this, chain the true_value and false_value methods onto your field’s definition:

djing_admin/app/Djing/User.py
from djing.core.Fields.Boolean import Boolean
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Boolean.make("Active").true_value("on").false_value("off"),
        ]

BooleanGroup

The BooleanGroup field may be used to group a set of Boolean checkboxes, which are then stored as JSON key-values in the database column they represent. You may create a BooleanGroup field by providing a set of keys and labels for each option:

djing_admin/app/Djing/User.py
from djing.core.Fields.BooleanGroup import BooleanGroup
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            BooleanGroup.make("Permissions").options({
                "create": "Create",
                "read": "Read",
                "update": "Update",
                "delete": "Delete",
            }),
        ]

Color

The Color field generates a color picker using the HTML5 color input element:

djing_admin/app/Djing/User.py
from djing.core.Fields.Color import Color
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Color.make("Color", "label_color"),
        ]

Currency

The Currency field generates a Number field that is automatically formatted using the babel.numbers python package. Djing will use USD as the default currency; however, this can be changed by modifying the djing_admin.config.djing.pyfile at currency value:

djing_admin/app/Djing/User.py
from djing.core.Fields.Currency import Currency
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Currency.make("Price"),
        ]

You may override the currency on a per-field basis using the currency method:

djing_admin/app/Djing/User.py
from djing.core.Fields.Currency import Currency
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Currency.make("Price").currency("USD"),
        ]

Date

The Date field may be used to store a date value (without time). For more information about dates and timezones within Djing, check out the additional date / timezone documentation

djing_admin/app/Djing/User.py
from djing.core.Fields.Date import Date
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Date.make("Birth Date"),
        ]

DateTime

The DateTime field may be used to store a date-time value. For more information about dates and timezones within Djing, check out the additional date / timezone documentation:

djing_admin/app/Djing/User.py
from djing.core.Fields.DateTime import DateTime
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            DateTime.make("Created At"),
        ]

Email

The Email field may be used to display a column with a mailto: link on the index and detail views:

djing_admin/app/Djing/User.py
from djing.core.Fields.Email import Email
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Email.make("Email Address", "email_address"),
        ]

File

To learn more about defining file fields and handling uploads, please refer to the comprehensive file field documentation.

djing_admin/app/Djing/User.py
from djing.core.Fields.File import File
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            File.make("Attachment"),
        ]

Image

The Image field extends the File field and accepts the same options and configurations. The Image field, unlike the File field, will display a thumbnail preview of the underlying image when viewing the resource:

djing_admin/app/Djing/User.py
from djing.core.Fields.Image import Image
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Image.make("Photo"),
        ]

By default, the Image field allows the user to download the linked file. To disable downloads, you may use the disable_download method on the field definition:

djing_admin/app/Djing/User.py
from djing.core.Fields.Image import Image
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Image.make("Photo").disable_download(),
        ]

Gravatar

The Gravatar field does not correspond to any column in your application’s database. Instead, it will display the “Gravatar” image of the model it is associated with.

By default, the Gravatar URL will be generated based on the value of the model’s email column. However, if your user’s email addresses are not stored in the email column, you may pass a custom column name to the field’s make method:

djing_admin/app/Djing/User.py
from djing.core.Fields.Gravatar import Gravatar
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Gravatar.make("Avatar", "email_address"),
        ]

You may use the squared method to display the image’s thumbnail with squared edges. Additionally, you may use the rounded method to display the images with fully-rounded edges:

djing_admin/app/Djing/User.py
from djing.core.Fields.Gravatar import Gravatar
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Gravatar.make("Avatar", "email_address").squared(),
        ]

Hidden

The Hidden field may be used to pass any value that doesn’t need to be changed by the user but is required for saving the resource:

djing_admin/app/Djing/User.py
from Illuminate.Support.Str import Str
from djing.core.Fields.Hidden import Hidden
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Hidden.make("Slug"),
            Hidden.make("Slug").default(Str.random(64)),
        ]

Combined with default values, Hidden fields are useful for passing things like related IDs to your forms:

djing_admin/app/Djing/User.py
from Illuminate.Support.Str import Str
from djing.core.Fields.Hidden import Hidden
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Hidden.make("User", "user_id").default(lambda request: request.user.id),
        ]

ID

The ID field represents the primary key of your resource’s database table. Typically, each Djing resource you define should contain an ID field. By default, the ID field assumes the underlying database column is named id; however, you may pass the column name as the second argument to the make method if necessary:

djing_admin/app/Djing/User.py
from djing.core.Fields.ID import ID
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            ID.make(),
            ID.make("ID", "id_column"),
        ]

If your application contains very large integer IDs, you may need to use the as_big_int method in order for the Djing client to correctly render the integer:

djing_admin/app/Djing/User.py
from djing.core.Fields.ID import ID
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            ID.make().as_big_int(),
        ]

KeyValue

The KeyValue field provides a convenient interface to edit flat, key-value data stored inside JSON column types. For example, you might store profile information inside a JSON column type named meta:

djing_admin/app/Djing/User.py
from djing.core.Fields.KeyValue import KeyValue
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            KeyValue.make("Meta").rules("json"),
        ]

— Customizing KeyValue Labels

You can customize the text values used in the component by calling the key_label, value_label, and action_text methods when defining the field. The action_text method customizes the “add row” button text:

djing_admin/app/Djing/User.py
from djing.core.Fields.KeyValue import KeyValue
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            KeyValue.make("Meta")
                .key_label("Item")
                .value_label("Label")
                .action_text("Add Item"),
        ]

If you would like to disable the user’s ability to edit the keys of the field, you may use the disable_editing_keys method to accomplish this. Disabling editing keys with the disable_editing_keys method will automatically disable adding rows as well:

djing_admin/app/Djing/User.py
from djing.core.Fields.KeyValue import KeyValue
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            KeyValue.make("Meta").disable_editing_keys(),
        ]

You may also remove the user’s ability to add new rows to the field by chaining the disable_editing_rows method onto the field’s definition:

djing_admin/app/Djing/User.py
from djing.core.Fields.KeyValue import KeyValue
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            KeyValue.make("Meta").disable_editing_rows(),
        ]

In addition, you may also wish to remove the user’s ability to delete exisiting rows in the field. You may accomplish this by invoking the disable_deleting_rows method when defining the field:

djing_admin/app/Djing/User.py
from djing.core.Fields.KeyValue import KeyValue
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            KeyValue.make("Meta").disable_deleting_rows(),
        ]

MultiSelect

The MultiSelect field provides a Select field that allows multiple selection options. This field pairs nicely with model attributes that are cast to array or equivalent:

djing_admin/app/Djing/User.py
from djing.core.Fields.MultiSelect import MultiSelect
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            MultiSelect.make("Options")
                .options({
                    "S": "Small",
                    "M": "Medium",
                    "L": "Large",
                })
                .display_using_labels(),
        ]

You may also display multi-select options in groups by providing an array structure that contains keys and label / group pairs:

djing_admin/app/Djing/User.py
from djing.core.Fields.MultiSelect import MultiSelect
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            MultiSelect.make("Options")
                .options({
                    "MS": {"label": "Small", "group": "Men's Sizes"},
                    "MM": {"label": "Medium", "group": "Men's Sizes"},
                    "WS": {"label": "Small", "group": "Women's Sizes"},
                    "WM": {"label": "Medium", "group": "Women's Sizes"},
                })
                .display_using_labels(),
        ]

Number

The Number field provides an input control with a type attribute of number:

djing_admin/app/Djing/User.py
from djing.core.Fields.Number import Number
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Number.make("Price"),
        ]

You may use the min, max, and step methods to set the corresponding HTML attributes on the generated input control:

djing_admin/app/Djing/User.py
from djing.core.Fields.Number import Number
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Number.make("Price").min(1).max(1000).step(0.01),
        ]

Password

The Password field provides an input control with a type attribute of password:

djing_admin/app/Djing/User.py
from djing.core.Fields.Password import Password
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Password.make("Password"),
        ]

The Password field will automatically preserve the password that is currently stored in the database if the incoming password field is empty. Therefore, a typical password field definition might look like the following:

djing_admin/app/Djing/User.py
from djing.core.Fields.Password import Password
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Password.make("Password")
                .only_on_forms()
                .creation_rules("required", "min:8")
                .update_rules("nullable", "min:8"),
        ]

PasswordConfirmation

The PasswordConfirmation field provides an input that can be used for confirming another Password field. This field will only be shown on forms and will not attempt to hydrate an underlying attribute on the Eloquent model:

djing_admin/app/Djing/User.py
from djing.core.Fields.PasswordConfirmation import PasswordConfirmation
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            PasswordConfirmation.make("Password Confirmation"),
        ]

When using this field, you should define the appropriate validation rules on the corresponding Password field:

djing_admin/app/Djing/User.py
from djing.core.Fields.Password import Password
from djing.core.Fields.PasswordConfirmation import PasswordConfirmation
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Password.make("Password")
                .only_on_forms()
                .creation_rules("required", "min:8")
                .update_rules("nullable", "min:8"),

            PasswordConfirmation.make("Password Confirmation"),
        ]

Slug

Sometimes you may need to generate a unique, human-readable identifier based on the contents of another field, such as when generating a “slug” for a blog post title. You can automatically generate these “slugs” using the Slug field:

djing_admin/app/Djing/User.py
from djing.core.Fields.Slug import Slug
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Slug.make("Slug").from_slug("Title"),
        ]

By default, the field will convert a string like ‘My Blog Post’ to a slug like ‘my-blog-post’. If you would like the field to use underscores instead of dashes, you may use the separator method to define your own custom “separator”:

djing_admin/app/Djing/User.py
from djing.core.Fields.Slug import Slug
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Slug.make("Slug").from_slug("Title").seperator("_"),
        ]

Select

The Select field may be used to generate a drop-down select menu. The Select menu’s options may be defined using the options method:

djing_admin/app/Djing/User.py
from djing.core.Fields.Select import Select
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Select.make("Options")
                .options({
                    "S": "Small",
                    "M": "Medium",
                    "L": "Large",
                }),
        ]

On the resource index and detail pages, the Select field’s “key” value will be displayed. If you would like to display the labels instead, you may use the display_using_labels method:

djing_admin/app/Djing/User.py
from djing.core.Fields.Select import Select
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Select.make("Options")
                .options({
                    "S": "Small",
                    "M": "Medium",
                    "L": "Large",
                })
                .display_using_labels(),
        ]

You may also display Select options in groups by providing an array structure that contains keys and label / group pairs:

djing_admin/app/Djing/User.py
from djing.core.Fields.Select import Select
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Select.make("Options")
                .options({
                    "MS": {"label": "Small", "group": "Men's Sizes"},
                    "MM": {"label": "Medium", "group": "Men's Sizes"},
                    "WS": {"label": "Small", "group": "Women's Sizes"},
                    "WM": {"label": "Medium", "group": "Women's Sizes"},
                })
                .display_using_labels(),
        ]

Text

The Text field provides an input control with a type attribute of text:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Name"),
        ]

Text fields may be further customized by setting any attribute on the field. This can be done by calling the with_meta method and providing an extra_attributes array containing key / value pairs of HTML attributes:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Name").with_meta({
                "extra_attributes": {
                    "aria-label": "name",
                }
            }),
        ]

Setting max_length on Text Fields

You may wish to indicate to the user that the content of a Text field should be kept within a certain length. You can do this by using the max_length method on the field:

djing_admin/app/Djing/User.py
from djing.core.Fields.Text import Text
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Name").max_length(250),
        ]

Textarea

The Textarea field provides a textarea control:

djing_admin/app/Djing/User.py
from djing.core.Fields.Textarea import Textarea
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Textarea.make("Description"),
        ]

By default, Textarea fields will not display their content when viewing a resource’s detail page. Instead, the contents of the field will be hidden behind a “Show Content” link, which will reveal the content when clicked. However, if you would like, you may specify that the Textarea field should always display its content by invoking the always_show method on the field:

djing_admin/app/Djing/User.py
from djing.core.Fields.Textarea import Textarea
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Textarea.make("Description").always_show(),
        ]

You may specify the Textarea height by invoking the rows method on the field:

djing_admin/app/Djing/User.py
from djing.core.Fields.Textarea import Textarea
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Textarea.make("Description").rows(3),
        ]

URL

The URL field renders URLs as clickable links instead of plain text:

djing_admin/app/Djing/User.py
from djing.core.Fields.URL import URL
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            URL.make("GitHub URL"),
        ]

The URL field also supports customizing the generated link’s text by invoking the display_using method when defining the field. The display_using method accepts a closure that should return the link’s text:

djing_admin/app/Djing/User.py
from djing.core.Fields.URL import URL
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            URL.make("Receipt").display_using(
                lambda request: f"{request.user.name}'s Receipt"
            ),
        ]

By providing a closure as the second argument to the URL field, you may use the field to render a link for a computed value that does not necessarily correspond to a column within the associated model’s database table:

djing_admin/app/Djing/User.py
from djing.core.Fields.URL import URL
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.app.Djing.Resource import Resource as DjingResource

class User(DjingResource):
    # ...

    def fields(self, request: DjingRequest):
        return [
            URL.make("Receipt", lambda: self.receipt),
        ]

Last updated