registrar¶
AdminModelRegistrar
¶
Class that handles the registration and configuration of Django admin classes for models.
This class provides a centralized way to register models with the Django admin site and manage their display properties (list_display, list_filter, etc.). It handles both regular Django models and polymorphic models.
General flow: 1. create prototype models (_admin_class_factory) 2. register models (register_models) 2.1. set defaults for fields to show up in admin(init method of listadmin mixin) 3. Modify fields with append methods once admin classes have been registered.
NOTE: This needs a fundamentally different approach. The Sync() idea is good, because¶
we need to make sure the instances and the adminclasses actually have the same¶
attributes or you get errors like this:¶
: (admin.E108) The¶
value of 'list_display[5]' refers to 'created_at', which is not a callable, an¶
attribute of 'AdminClass_ProjectAlternateName', or an attribute or method on¶
'project_tracker.ProjectAlternateName'.¶
As of right now (this commit) all edits using the methods for the registrar are¶
happenin on the widget, not the admin class.¶
They need to be syncd! And the sync code needs to be updated to sync changes from¶
the instance to the admin class prototype.¶
Source code in src/django_admin_magic/registrar.py
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 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 | |
__init__(app_label=None, app_labels=None, auto_discover=False)
¶
Initialize the registrar for specific Django app(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_label
|
str
|
The label of a single Django app to register models for. |
None
|
app_labels
|
List[str]
|
List of Django app labels to register models for. |
None
|
auto_discover
|
bool
|
Whether to auto-discover and register all installed apps. |
False
|
Source code in src/django_admin_magic/registrar.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
add_admin_method(model, method_name, method_func, *, short_description=None, is_action=False)
¶
Add a method to the admin class for a model.
This method allows adding custom methods to the admin class, optionally marking them as actions and setting their short descriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
InclusiveModelType
|
The model to add the method to |
required |
method_name
|
str
|
The name to give the method |
required |
method_func
|
Callable
|
The method to add (should be a function) |
required |
short_description
|
str | None
|
Optional description for the method in admin interface |
None
|
is_action
|
bool
|
Whether to add this method to the actions list |
False
|
Example
def mark_as_special(modeladmin, request, queryset): queryset.update(is_special=True)
registrar.add_admin_method( MyModel, "mark_as_special", mark_as_special, short_description="Mark selected items as special", is_action=True )
Source code in src/django_admin_magic/registrar.py
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 | |
append_filter_display(model, list_filter)
¶
Add filter fields to the admin display.
This method adds fields to the list_filter property of the admin class. It follows the same pattern as list_display management: 1. Validates fields exist on the model 2. Maintains field order while preventing duplicates 3. Updates both admin class and registered instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model] | Model | PolymorphicModelBase
|
The model to add filters to |
required |
list_filter
|
list[str] | tuple[str]
|
List or tuple of field names to add as filters |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If model not found in registered classes |
FieldDoesNotExist
|
If field doesn't exist on model |
Source code in src/django_admin_magic/registrar.py
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 | |
append_inline(model, inline_class)
¶
Add an inline class to a model's admin.
This method: 1. Gets or creates the inlines list 2. Converts tuple to list if needed 3. Appends the new inline 4. Updates both admin class and registered instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
InclusiveModelType
|
The model to add the inline to |
required |
inline_class
|
type[TabularInline | StackedInline]
|
The inline class to add |
required |
Source code in src/django_admin_magic/registrar.py
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 | |
append_list_display(model, list_display)
¶
Add fields to the end of the admin display list.
This method: 1. Validates that all fields exist on the model 2. Maintains field order while preventing duplicates 3. Updates both the admin class and registered instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
InclusiveModelType
|
The model to modify the admin display for |
required |
list_display
|
list[str] | tuple[str]
|
Fields to add to the display list |
required |
Source code in src/django_admin_magic/registrar.py
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 | |
model_iterator()
¶
Iterates through models and creates the associated admin class. Convenience method to create admin classes for models without having to explicitly create them in each app's admin.py file.
Source code in src/django_admin_magic/registrar.py
237 238 239 240 241 242 243 244 245 246 247 248 | |
prepend_list_display(model, list_display)
¶
Add fields to the start of the admin display list.
This method: 1. Validates that all fields exist on the model 2. Removes any existing instances of the fields 3. Prepends fields to the start of the list 4. Updates both the admin class and registered instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
InclusiveModelType
|
The model to modify the admin display for |
required |
list_display
|
str | list[str] | tuple[str]
|
Field(s) to add to the start of the display list. Can be a single string or list/tuple of strings. |
required |
Source code in src/django_admin_magic/registrar.py
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 | |
register_all_discovered_apps()
classmethod
¶
Class method to create a registrar that auto-discovers and registers all apps.
This is useful for users who want to register all their models automatically without specifying individual app labels.
Returns:
| Type | Description |
|---|---|
AdminModelRegistrar
|
AdminModelRegistrar instance configured for auto-discovery |
Source code in src/django_admin_magic/registrar.py
195 196 197 198 199 200 201 202 203 204 205 206 207 | |
register_app(app_label)
classmethod
¶
Class method to create a registrar for a single app.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_label
|
str
|
The app label to register |
required |
Returns:
| Type | Description |
|---|---|
AdminModelRegistrar
|
AdminModelRegistrar instance configured for the specified app |
Source code in src/django_admin_magic/registrar.py
223 224 225 226 227 228 229 230 231 232 233 234 235 | |
register_apps(app_labels)
classmethod
¶
Class method to create a registrar for specific app labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_labels
|
list[str]
|
List of app labels to register |
required |
Returns:
| Type | Description |
|---|---|
AdminModelRegistrar
|
AdminModelRegistrar instance configured for the specified apps |
Source code in src/django_admin_magic/registrar.py
209 210 211 212 213 214 215 216 217 218 219 220 221 | |
register_models()
¶
Register models with the Django admin site.
This method handles the registration process for all models in the app. It ensures that: 1. Each model gets the correct admin class type (regular or polymorphic) 2. List display and filter properties are properly initialized
Source code in src/django_admin_magic/registrar.py
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 | |
return_admin_class_for_model(model)
¶
Return the admin class for a model.
This method retrieves the admin class associated with a model from the class dictionary. It handles both class and instance model references.
The returned admin class can be modified to customize various aspects of the admin interface. See Django's ModelAdmin documentation for all available options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
InclusiveModelType
|
The model to get the admin class for |
required |
Returns:
| Type | Description |
|---|---|
|
The admin class for the model |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the model is not found in the registered classes |
Example Usage
admin_class = registrar.return_admin_class_for_model(MyModel) admin_class.list_filter += ["is_custom_component", "category"] admin_class.show_full_result_count = False admin_class.paginator = TimeLimitedPaginator
Source code in src/django_admin_magic/registrar.py
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 | |
update_list_select_related(model, list_select_related)
¶
Update the list_select_related property for a model's admin class.
This method allows setting the list_select_related property to optimize database queries for the admin changelist view. It can be: - True: Select all foreign key relationships - False: Don't use any JOINs - List/tuple of field names: Only JOIN the specified relationships
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
InclusiveModelType
|
The model whose admin class should be updated |
required |
list_select_related
|
list[str] | tuple[str, ...] | bool
|
The value to set for list_select_related |
required |
Example
To select all foreign keys¶
registrar.update_list_select_related(MyModel, True)
To select specific foreign keys¶
registrar.update_list_select_related(MyModel, ['author', 'category'])
To disable all automatic JOINs¶
registrar.update_list_select_related(MyModel, False)
Source code in src/django_admin_magic/registrar.py
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 | |
NoOpRegistrar
¶
Registrar that performs no operations.
Used when auto-registration is disabled (e.g., during migrations or when admin is not installed). Provides the same API surface as the real registrar but all methods are safe no-ops to avoid import-time side effects.
Source code in src/django_admin_magic/registrar.py
29 30 31 32 33 34 35 36 37 38 39 40 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 | |
PolymorphicAdminModelRegistrar
¶
Bases: AdminModelRegistrar
An extension of AdminModelRegistrar that handles polymorphic models.
Source code in src/django_admin_magic/registrar.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | |