Fields
starlette_admin.fields
BaseField
dataclass
Base class for fields
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Field name, same as attribute name in your model |
required |
label
|
Optional[str]
|
Field label |
None
|
help_text
|
Optional[str]
|
Hint message to display in forms |
None
|
type
|
Optional[str]
|
Field type, unique key used to define the field |
None
|
disabled
|
Optional[bool]
|
Disabled in forms |
False
|
read_only
|
Optional[bool]
|
Read only in forms |
False
|
id
|
str
|
Unique id, used to represent field instance |
''
|
search_builder_type
|
Optional[str]
|
datatable columns.searchBuilderType, For more information click here |
'default'
|
required
|
Optional[bool]
|
Indicate if the fields is required |
False
|
exclude_from_list
|
Optional[bool]
|
Control field visibility in list page |
False
|
exclude_from_detail
|
Optional[bool]
|
Control field visibility in detail page |
False
|
exclude_from_create
|
Optional[bool]
|
Control field visibility in create page |
False
|
exclude_from_edit
|
Optional[bool]
|
Control field visibility in edit page |
False
|
searchable
|
Optional[bool]
|
Indicate if the fields is searchable |
True
|
orderable
|
Optional[bool]
|
Indicate if the fields is orderable |
True
|
render_function_key
|
str
|
Render function key inside the global |
'text'
|
form_template
|
str
|
template for rendering this field in creation and edit page |
'forms/input.html'
|
display_template
|
str
|
template for displaying this field in detail page |
'displays/text.html'
|
Source code in starlette_admin/fields.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
additional_css_links(request, action)
Returns a list of CSS file URLs to include for the current request action.
additional_js_links(request, action)
Returns a list of JavaScript file URLs to include for the current request action.
dict()
input_params()
parse_form_data(request, form_data, action)
async
Extracts the value of this field from submitted form data.
parse_obj(request, obj)
async
Extracts the value of this field from a model instance.
By default, this function returns the value of the attribute with the name self.name
from obj
.
However, this function can be overridden to provide custom logic for computing the value of a field.
Example
# Suppose we have a `User` model with `id`, `first_name`, and `last_name` fields.
# We define a custom field called `MyCustomField` to compute the full name of the user:
class MyCustomField(StringField):
async def parse_obj(self, request: Request, obj: Any) -> Any:
return f"{obj.first_name} {obj.last_name}" # Returns the full name of the user
# Then, We can define our view as follows
class UserView(ModelView):
fields = ["id", MyCustomField("full_name")]
Source code in starlette_admin/fields.py
serialize_none_value(request, action)
async
Formats a None value for sending to the frontend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The current request object. |
required |
action
|
RequestAction
|
The current request action. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The formatted None value. |
Source code in starlette_admin/fields.py
serialize_value(request, value, action)
async
Formats a value for sending to the frontend based on the current request action.
Important
Make sure this value is JSON Serializable for RequestAction.LIST and RequestAction.API
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The current request object. |
required |
value
|
Any
|
The value to format. |
required |
action
|
RequestAction
|
The current request action. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The formatted value. |
Source code in starlette_admin/fields.py
BooleanField
dataclass
Bases: BaseField
This field displays the true/false
value of a boolean property.
Source code in starlette_admin/fields.py
IntegerField
dataclass
Bases: NumberField
This field is used to represent the value of properties that store integer numbers. Erroneous input is ignored and will not be accepted as a value.
Source code in starlette_admin/fields.py
DecimalField
dataclass
Bases: NumberField
This field is used to represent the value of properties that store decimal numbers. Erroneous input is ignored and will not be accepted as a value.
Source code in starlette_admin/fields.py
FloatField
dataclass
Bases: StringField
A text field, except all input is coerced to an float. Erroneous input is ignored and will not be accepted as a value.
Source code in starlette_admin/fields.py
StringField
dataclass
Bases: BaseField
This field is used to represent any kind of short text content.
Source code in starlette_admin/fields.py
TextAreaField
dataclass
Bases: StringField
This field is used to represent any kind of long text content. For short text contents, use StringField
Source code in starlette_admin/fields.py
TinyMCEEditorField
dataclass
Bases: TextAreaField
A field that provides a WYSIWYG editor for long text content using the TinyMCE library.
This field can be used as an alternative to the TextAreaField to provide a more sophisticated editor for user input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
version_tinymce
|
str
|
TinyMCE version |
'6.4'
|
version_tinymce_jquery
|
str
|
TinyMCE jQuery version |
'2.0'
|
height
|
int
|
Height of the editor |
300
|
menubar
|
Union[bool, str]
|
Show/hide the menubar in the editor |
False
|
statusbar
|
bool
|
Show/hide the statusbar in the editor |
False
|
toolbar
|
str
|
Toolbar options to show in the editor |
'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat'
|
content_style
|
str
|
CSS style to apply to the editor content |
'body { font-family: -apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif; font-size: 14px; -webkit-font-smoothing: antialiased; }'
|
extra_options
|
Dict[str, Any]
|
Other options to pass to TinyMCE |
dict()
|
Source code in starlette_admin/fields.py
extra_options: Dict[str, Any] = dc_field(default_factory=dict)
class-attribute
instance-attribute
For more options, see the TinyMCE | Documentation
TagsField
dataclass
Bases: BaseField
This field is used to represent the value of properties that store a list of
string values. Render as select2
tags input.
Source code in starlette_admin/fields.py
EmailField
dataclass
Bases: StringField
This field is used to represent a text content that stores a single email address.
Source code in starlette_admin/fields.py
URLField
dataclass
Bases: StringField
This field is used to represent a text content that stores a single URL.
Source code in starlette_admin/fields.py
PhoneField
dataclass
Bases: StringField
A StringField, except renders an <input type="phone">
.
Source code in starlette_admin/fields.py
ColorField
dataclass
Bases: StringField
A StringField, except renders an <input type="color">
.
Source code in starlette_admin/fields.py
PasswordField
dataclass
Bases: StringField
A StringField, except renders an <input type="password">
.
Source code in starlette_admin/fields.py
EnumField
dataclass
Bases: StringField
Enumeration Field.
It takes a python enum.Enum
class or a list of (value, label) pairs.
It can also be a list of only values, in which case the value is used as the label.
Example:
class Status(str, enum.Enum):
NEW = "new"
ONGOING = "ongoing"
DONE = "done"
class MyModel:
status: Optional[Status] = None
class MyModelView(ModelView):
fields = [EnumField("status", enum=Status)]
```python
class MyModel:
language: str
class MyModelView(ModelView):
fields = [
EnumField(
"language",
choices=[("cpp", "C++"), ("py", "Python"), ("text", "Plain Text")],
)
]
```
Source code in starlette_admin/fields.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
|
TimeZoneField
dataclass
Bases: EnumField
This field is used to represent the name of a timezone (eg. Africa/Porto-Novo)
Source code in starlette_admin/fields.py
CountryField
dataclass
Bases: EnumField
This field is used to represent the name that corresponds to the country code stored in your database
Source code in starlette_admin/fields.py
CurrencyField
dataclass
Bases: EnumField
This field is used to represent a value that stores the 3-letter ISO 4217 code of currency
Source code in starlette_admin/fields.py
DateTimeField
dataclass
Bases: NumberField
This field is used to represent a value that stores a python datetime.datetime object Parameters: search_format: moment.js format to send for searching. Use None for iso Format output_format: display output format
Source code in starlette_admin/fields.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 |
|
DateField
dataclass
Bases: DateTimeField
This field is used to represent a value that stores a python datetime.date object Parameters: search_format: moment.js format to send for searching. Use None for iso Format output_format: Set display output format
Source code in starlette_admin/fields.py
TimeField
dataclass
Bases: DateTimeField
This field is used to represent a value that stores a python datetime.time object Parameters: search_format: Format to send for search. Use None for iso Format output_format: Set display output format
Source code in starlette_admin/fields.py
ArrowField
dataclass
Bases: DateTimeField
This field is used to represent sqlalchemy_utils.types.arrow.ArrowType
Source code in starlette_admin/fields.py
JSONField
dataclass
Bases: BaseField
This field render jsoneditor and represent a value that stores python dict object. Erroneous input is ignored and will not be accepted as a value.
Source code in starlette_admin/fields.py
FileField
dataclass
Bases: BaseField
Renders a file upload field.
This field is used to represent a value that stores starlette UploadFile object.
For displaying value, this field wait for three properties which is filename
,
content-type
and url
. Use multiple=True
for multiple file upload
When user ask for delete on editing page, the second part of the returned tuple is True.
Source code in starlette_admin/fields.py
ImageField
dataclass
RelationField
dataclass
Bases: BaseField
A field representing a relation between two data models.
This field should not be used directly; instead, use either the HasOne or HasMany fields to specify a relation between your models.
Important
It is important to add both models in your admin interface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identity
|
Optional[str]
|
Foreign ModelView identity |
None
|
Example
class Author:
id: Optional[int]
name: str
books: List["Book"]
class Book:
id: Optional[int]
title: str
author: Optional["Author"]
class AuthorView(ModelView):
fields = [
IntegerField("id"),
StringField("name"),
HasMany("books", identity="book"),
]
class BookView(ModelView):
fields = [
IntegerField("id"),
StringField("title"),
HasOne("author", identity="author"),
]
...
admin.add_view(AuthorView(Author, identity="author"))
admin.add_view(BookView(Book, identity="book"))
...
Source code in starlette_admin/fields.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
|
HasOne
dataclass
HasMany
dataclass
ListField
dataclass
Bases: BaseField
Encapsulate an ordered list of multiple instances of the same field type, keeping data as a list.
Usage
Source code in starlette_admin/fields.py
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 |
|
CollectionField
dataclass
Bases: BaseField
This field represents a collection of others fields. Can be used to represent embedded mongodb document.
Usage
CollectionField("config", fields=[StringField("key"), IntegerField("value", help_text="multiple of 5")]),
Source code in starlette_admin/fields.py
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 |
|