| | 489 | ``ModelAdmin`` methods |
| | 490 | ------------------------ |
| | 491 | |
| | 492 | ModelAdmin subclasses can redefine specific methods in order to customize even more the admin site : |
| | 493 | |
| | 494 | ``dynamic_XXX_choices`` |
| | 495 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| | 496 | |
| | 497 | the ``dynamic_XXX_choices`` method, where XXX is a ForeignKey or ManyToMany field of your model, allows the admin site to filter the available choices for that field. |
| | 498 | |
| | 499 | An example usage of it is :: |
| | 500 | |
| | 501 | # Model |
| | 502 | |
| | 503 | class Book(models.Model): |
| | 504 | title = models.CharField(maxlength=100) |
| | 505 | author = models.ForeignKey(Author) |
| | 506 | |
| | 507 | # Admin |
| | 508 | |
| | 509 | class BookAdmin(ModelAdmin): |
| | 510 | def dynamic_author_choices(self, request, model): |
| | 511 | # Default implementation: |
| | 512 | # return book.author_set.all() |
| | 513 | if request.user.is_superuser: |
| | 514 | return model.objects.all() |
| | 515 | return model.objects.filter(use_in_admin=True) |
| | 516 | |
| | 517 | |