Views
starlette_admin.views
BaseView
Base class for all views
Attributes:
Name | Type | Description |
---|---|---|
label |
str
|
Label of the view to be displayed. |
icon |
Optional[str]
|
Icon to be displayed for this model in the admin. Only FontAwesome names are supported. |
Source code in starlette_admin/views.py
is_accessible(request)
Override this method to add permission checks. Return True if current user can access this view
is_active(request)
DropDown
Bases: BaseView
Group views inside a dropdown
Example
Source code in starlette_admin/views.py
Link
Bases: BaseView
Add arbitrary hyperlinks to the menu
Source code in starlette_admin/views.py
CustomView
Bases: BaseView
Add your own views (not tied to any particular model). For example, a custom home page that displays some analytics data.
Attributes:
Name | Type | Description |
---|---|---|
path |
Route path |
|
template_path |
Path to template file |
|
methods |
HTTP methods |
|
name |
Route name |
|
add_to_menu |
Display to menu or not |
Example
Source code in starlette_admin/views.py
render(request, templates)
async
Default methods to render view. Override this methods to add your custom logic.
Source code in starlette_admin/views.py
BaseModelView
Bases: BaseView
Base administrative view. Derive from this class to implement your administrative interface piece.
Attributes:
Name | Type | Description |
---|---|---|
identity |
Optional[str]
|
Unique identity to identify the model associated to this view. Will be used for URL of the endpoints. |
name |
Optional[str]
|
Name of the view to be displayed |
fields |
Sequence[BaseField]
|
List of fields |
pk_attr |
Optional[str]
|
Primary key field name |
form_include_pk |
bool
|
Indicates whether the primary key should be included in create and edit forms. Default to False. |
exclude_fields_from_list |
Sequence[str]
|
List of fields to exclude in List page. |
exclude_fields_from_detail |
Sequence[str]
|
List of fields to exclude in Detail page. |
exclude_fields_from_create |
Sequence[str]
|
List of fields to exclude from creation page. |
exclude_fields_from_edit |
Sequence[str]
|
List of fields to exclude from editing page. |
searchable_fields |
Optional[Sequence[str]]
|
List of searchable fields. |
sortable_fields |
Optional[Sequence[str]]
|
List of sortable fields. |
export_fields |
Optional[Sequence[str]]
|
List of fields to include in exports. |
fields_default_sort |
Optional[Sequence[Union[Tuple[str, bool], str]]]
|
Initial order (sort) to apply to the table.
Should be a sequence of field names or a tuple of
(field name, True/False to indicate the sort direction).
For example:
|
export_types |
Sequence[ExportType]
|
A list of available export filetypes. Available
exports are |
column_visibility |
bool
|
Enable/Disable column visibility extension |
search_builder |
bool
|
Enable/Disable search builder extension |
page_size |
int
|
Default number of items to display in List page pagination.
Default value is set to |
page_size_options |
Sequence[int]
|
Pagination choices displayed in List page.
Default value is set to |
responsive_table |
bool
|
Enable/Disable responsive extension |
save_state |
bool
|
Enable/Disable state saving |
datatables_options |
Dict[str, Any]
|
Dict of Datatables options. These will overwrite any default options set for the datatable. |
list_template |
str
|
List view template. Default is |
detail_template |
str
|
Details view template. Default is |
create_template |
str
|
Edit view template. Default is |
edit_template |
str
|
Edit view template. Default is |
actions |
Optional[Sequence[str]]
|
List of actions |
additional_js_links |
Optional[List[str]]
|
A list of additional JavaScript files to include. |
additional_css_links |
Optional[List[str]]
|
A list of additional CSS files to include. |
Source code in starlette_admin/views.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 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 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 |
|
after_create(request, obj)
async
This hook is called after a new item is successfully created.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed. |
required |
obj
|
Any
|
The newly created object. |
required |
after_delete(request, obj)
async
This hook is called after an item is successfully deleted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed. |
required |
obj
|
Any
|
The deleted object. |
required |
after_edit(request, obj)
async
This hook is called after an item is successfully edited.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed. |
required |
obj
|
Any
|
The edited object. |
required |
before_create(request, data, obj)
async
This hook is called before a new item is created.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed. |
required |
data
|
Dict[str, Any]
|
Dict values contained converted form data. |
required |
obj
|
Any
|
The object about to be created. |
required |
Source code in starlette_admin/views.py
before_delete(request, obj)
async
This hook is called before an item is deleted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed. |
required |
obj
|
Any
|
The object about to be deleted. |
required |
before_edit(request, data, obj)
async
This hook is called before an item is edited.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed. |
required |
data
|
Dict[str, Any]
|
Dict values contained converted form data |
required |
obj
|
Any
|
The object about to be edited. |
required |
Source code in starlette_admin/views.py
can_create(request)
can_delete(request)
can_edit(request)
can_view_details(request)
count(request, where=None)
abstractmethod
async
Count items Parameters: request: The request being processed where: Can be dict for complex query
or plain text for full searchSource code in starlette_admin/views.py
create(request, data)
abstractmethod
async
Create item Parameters: request: The request being processed data: Dict values contained converted form data Returns: Any: Created Item
Source code in starlette_admin/views.py
delete(request, pks)
abstractmethod
async
Bulk delete items Parameters: request: The request being processed pks: List of primary keys
edit(request, pk, data)
abstractmethod
async
Edit item Parameters: request: The request being processed pk: Primary key data: Dict values contained converted form data Returns: Any: Edited Item
Source code in starlette_admin/views.py
find_all(request, skip=0, limit=100, where=None, order_by=None)
abstractmethod
async
Find all items Parameters: request: The request being processed where: Can be dict for complex query
or plain text for full search skip: should return values start from position skip+1 limit: number of maximum items to return order_by: order data clauses in form["id asc", "name desc"]
Source code in starlette_admin/views.py
find_by_pk(request, pk)
abstractmethod
async
Find one item Parameters: request: The request being processed pk: Primary key
find_by_pks(request, pks)
abstractmethod
async
Find many items Parameters: request: The request being processed pks: List of Primary key
get_all_actions(request)
async
Return a list of allowed batch actions
Source code in starlette_admin/views.py
get_all_row_actions(request)
async
Return a list of allowed row actions
Source code in starlette_admin/views.py
get_fields_list(request, action=RequestAction.LIST)
Return a list of field instances to display in the specified view action.
This function excludes fields with corresponding exclude flags, which are
determined by the exclude_fields_from_*
attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed. |
required |
action
|
RequestAction
|
The type of action being performed on the view. |
LIST
|
Source code in starlette_admin/views.py
get_serialized_pk_value(request, obj)
async
Return serialized value of the primary key.
Note
The returned value should be JSON-serializable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
The request being processed |
required |
obj
|
Any
|
object to get primary key of |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
Serialized value of a PK. |
Source code in starlette_admin/views.py
handle_action(request, pks, name)
async
Handle action with name
.
Raises:
ActionFailed: to display meaningfully error
Source code in starlette_admin/views.py
handle_row_action(request, pk, name)
async
Handle row action with name
.
Raises:
ActionFailed: to display meaningfully error
Source code in starlette_admin/views.py
is_action_allowed(request, name)
async
Verify if action with name
is allowed.
Override this method to allow or disallow actions based
on some condition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Action name |
required |
request
|
Request
|
Starlette request |
required |
Source code in starlette_admin/views.py
is_row_action_allowed(request, name)
async
Verify if the row action with name
is allowed.
Override this method to allow or disallow row actions based
on some condition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Row action name |
required |
request
|
Request
|
Starlette request |
required |
Source code in starlette_admin/views.py
repr(obj, request)
async
Return a string representation of the given object that can be displayed in the admin interface.
If the object has a custom representation method __admin_repr__
, it is used to generate the string. Otherwise,
the value of the object's primary key attribute is used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object to represent. |
required |
request
|
Request
|
The request being processed |
required |
Example
For example, the following implementation for a User
model will display
the user's full name instead of their primary key in the admin interface:
Source code in starlette_admin/views.py
select2_result(obj, request)
async
Returns an HTML-formatted string that represents the search results for a Select2 search box.
By default, this method returns a string that contains all the object's attributes in a list except relation and file attributes.
If the object has a custom representation method __admin_select2_repr__
, it is used to generate the
HTML-formatted string.
Note
The returned value should be valid HTML.
Danger
Escape your database value to avoid Cross-Site Scripting (XSS) attack.
You can use Jinja2 Template render with autoescape=True
.
For more information click here
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object returned by the |
required |
request
|
Request
|
The request being processed |
required |
Example
Here is an example implementation for a User
model
that includes the user's name and photo:
Source code in starlette_admin/views.py
select2_selection(obj, request)
async
Returns the HTML representation of an item selected by a user in a Select2 component.
By default, it simply calls select2_result()
.
Note
The returned value should be valid HTML.
Danger
Escape your database value to avoid Cross-Site Scripting (XSS) attack.
You can use Jinja2 Template render with autoescape=True
.
For more information click here
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
item returned by |
required |
request
|
Request
|
The request being processed |
required |
Source code in starlette_admin/views.py
serialize_field_value(value, field, action, request)
async
Format output value for each field.
Important
The returned value should be json serializable
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
attribute of item returned by |
required |
field
|
BaseField
|
Starlette Admin field for this attribute |
required |
action
|
RequestAction
|
Specify where the data will be used. Possible values are
|
required |
request
|
Request
|
The request being processed |
required |