class AdminResource(Model): label = "Admins" model = Admin icon = "fas fa-user" page_pre_title = "admin list" page_title = "admin model" filters = [ filters.Search( name="username", label="Name", search_mode="contains", placeholder="Search for username", ), filters.Date(name="created_at", label="CreatedAt"), ] fields = [ "id", "username", Field( name="password", label="Password", display=displays.InputOnly(), input_=inputs.Password(), ), Field(name="email", label="Email", input_=inputs.Email()), "created_at", ]
def _get_display_input_field(cls, field_name: str) -> Field: fields_map = cls.model._meta.fields_map field = fields_map.get(field_name) if not field: raise NoSuchFieldFound( f"Can't found field '{field_name}' in model {cls.model}") label = field_name null = field.null placeholder = field.description or "" display, input_ = displays.Display(), inputs.Input( placeholder=placeholder, null=null, default=field.default) if field.pk or field.generated: display, input_ = displays.Display(), inputs.DisplayOnly() elif isinstance(field, BooleanField): display, input_ = displays.Boolean(), inputs.Switch( null=null, default=field.default) elif isinstance(field, DatetimeField): if field.auto_now or field.auto_now_add: input_ = inputs.DisplayOnly() else: input_ = inputs.DateTime(null=null, default=field.default) display, input_ = displays.DatetimeDisplay(), input_ elif isinstance(field, DateField): display, input_ = displays.DateDisplay(), inputs.Date( null=null, default=field.default) elif isinstance(field, IntEnumFieldInstance): display, input_ = displays.Display(), inputs.Enum( field.enum_type, null=null, default=field.default) elif isinstance(field, CharEnumFieldInstance): display, input_ = displays.Display(), inputs.Enum( field.enum_type, enum_type=str, null=null, default=field.default) elif isinstance(field, JSONField): display, input_ = displays.Json(), inputs.Json(null=null) elif isinstance(field, TextField): display, input_ = displays.Display(), inputs.TextArea( placeholder=placeholder, null=null, default=field.default) elif isinstance(field, IntField): display, input_ = displays.Display(), inputs.Number( placeholder=placeholder, null=null, default=field.default) elif isinstance(field, ForeignKeyFieldInstance): display, input_ = displays.Display(), inputs.ForeignKey( field.related_model, null=null, default=field.default) field_name = field.source_field elif isinstance(field, ManyToManyFieldInstance): display, input_ = displays.InputOnly(), inputs.ManyToMany( field.related_model) return Field(name=field_name, label=label.title(), display=display, input_=input_)
class AdminResource(Model): label = "Admin" model = Admin icon = "fas fa-user" page_pre_title = "admin list" page_title = "admin model" filters = [ filters.Search( name="username", label="Name", search_mode="contains", placeholder="Search for username", ), filters.Date(name="created_at", label="CreatedAt"), ] fields = [ "id", "username", Field( name="password", label="Password", display=displays.InputOnly(), input_=inputs.Password(), ), Field(name="email", label="Email", input_=inputs.Email()), Field( name="avatar", label="Avatar", display=displays.Image(width="40"), input_=inputs.Image(null=True, upload=upload), ), "created_at", ] async def get_toolbar_actions(self, request: Request) -> List[ToolbarAction]: return [] async def cell_attributes(self, request: Request, obj: dict, field: Field) -> dict: if field.name == "id": return {"class": "bg-danger text-white"} return await super().cell_attributes(request, obj, field) async def get_actions(self, request: Request) -> List[Action]: return [] async def get_bulk_actions(self, request: Request) -> List[Action]: return []