| | 430 | ``InlineModelAdmin`` objects |
|---|
| | 431 | ============================ |
|---|
| | 432 | |
|---|
| | 433 | The admin interface has the ability to edit models on the same page as a |
|---|
| | 434 | parent model. These are called inlines. You can add them a model being |
|---|
| | 435 | specifing them in a ``ModelAdmin.inlines`` attribute:: |
|---|
| | 436 | |
|---|
| | 437 | class BookInline(admin.TabularInline): |
|---|
| | 438 | model = Book |
|---|
| | 439 | |
|---|
| | 440 | class AuthorAdmin(admin.ModelAdmin): |
|---|
| | 441 | inlines = [ |
|---|
| | 442 | BookInline, |
|---|
| | 443 | ] |
|---|
| | 444 | |
|---|
| | 445 | Django provides two subclasses of ``InlineModelAdmin`` and they are:: |
|---|
| | 446 | |
|---|
| | 447 | * ``TabularInline`` |
|---|
| | 448 | * ``StackedInline`` |
|---|
| | 449 | |
|---|
| | 450 | The difference between these two is merely the template used to render them. |
|---|
| | 451 | |
|---|
| | 452 | ``InlineModelAdmin`` options |
|---|
| | 453 | ----------------------------- |
|---|
| | 454 | |
|---|
| | 455 | The ``InlineModelAdmin`` class is a subclass of ``ModelAdmin`` so it inherits |
|---|
| | 456 | all the same functionality as well as some of its own: |
|---|
| | 457 | |
|---|
| | 458 | ``model`` |
|---|
| | 459 | ~~~~~~~~~ |
|---|
| | 460 | |
|---|
| | 461 | The model in which the inline is using. This is required. |
|---|
| | 462 | |
|---|
| | 463 | ``fk_name`` |
|---|
| | 464 | ~~~~~~~~~~~ |
|---|
| | 465 | |
|---|
| | 466 | The name of the foreign key on the model. In most cases this will be dealt |
|---|
| | 467 | with automatically, but ``fk_name`` must be specified explicitly if there are |
|---|
| | 468 | more than one foreign key to the same parent model. |
|---|
| | 469 | |
|---|
| | 470 | ``formset`` |
|---|
| | 471 | ~~~~~~~~~~~ |
|---|
| | 472 | |
|---|
| | 473 | This defaults to ``BaseInlineFormset``. Using your own formset can give you |
|---|
| | 474 | many possibilities of customization. Inlines are built around |
|---|
| | 475 | `model formsets`_. |
|---|
| | 476 | |
|---|
| | 477 | .. _model formsets: ../modelforms/# |
|---|
| | 478 | |
|---|
| | 479 | ``form`` |
|---|
| | 480 | ~~~~~~~~ |
|---|
| | 481 | |
|---|
| | 482 | The value for ``form`` is inherited from ``ModelAdmin``. This is what is |
|---|
| | 483 | passed through to ``formset_factory`` when creating the formset for this |
|---|
| | 484 | inline. |
|---|
| | 485 | |
|---|
| | 486 | ``extra`` |
|---|
| | 487 | ~~~~~~~~~ |
|---|
| | 488 | |
|---|
| | 489 | This controls the number of extra forms the formset will display in addition |
|---|
| | 490 | to the initial forms. See `extra in formsets`_ for more information. |
|---|
| | 491 | |
|---|
| | 492 | ``max_num`` |
|---|
| | 493 | ~~~~~~~~~~~ |
|---|
| | 494 | |
|---|
| | 495 | This controls the maximum number of forms to show in the inline. This doesn't |
|---|
| | 496 | directly corrolate to the number of objects, but can if the value is small |
|---|
| | 497 | enough. See `max_num in model formsets`_ for more information. |
|---|
| | 498 | |
|---|
| | 499 | ``template`` |
|---|
| | 500 | ~~~~~~~~~~~~ |
|---|
| | 501 | |
|---|
| | 502 | The template used to render the inline on the page. |
|---|
| | 503 | |
|---|
| | 504 | ``verbose_name`` |
|---|
| | 505 | ~~~~~~~~~~~~~~~~ |
|---|
| | 506 | |
|---|
| | 507 | An override to the ``verbose_name`` found in the model's inner ``Meta`` class. |
|---|
| | 508 | |
|---|
| | 509 | ``verbose_name_plural`` |
|---|
| | 510 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 511 | |
|---|
| | 512 | An override to the ``verbose_name_plural`` found in the model's inner ``Meta`` |
|---|
| | 513 | class. |
|---|
| | 514 | |
|---|
| | 515 | Working with a model with two or more foreign keys to the same parent model |
|---|
| | 516 | --------------------------------------------------------------------------- |
|---|
| | 517 | |
|---|
| | 518 | It is sometimes possible to have more than one foreign key to the same model. |
|---|
| | 519 | Take this model for instance:: |
|---|
| | 520 | |
|---|
| | 521 | class Friendship(models.Model): |
|---|
| | 522 | to_person = models.ForeignKey(Person, related_name="friends") |
|---|
| | 523 | from_person = models.ForeignKey(Person, related_name="from_friends") |
|---|
| | 524 | |
|---|
| | 525 | If you wanted to display an inline on the ``Person`` admin add/change pages |
|---|
| | 526 | you need to explicitly define the foreign key since it is unable to do so |
|---|
| | 527 | automatically:: |
|---|
| | 528 | |
|---|
| | 529 | class FriendshipInline(admin.TabularInline): |
|---|
| | 530 | model = Friendship |
|---|
| | 531 | fk_name = "to_person" |
|---|
| | 532 | |
|---|
| | 533 | class PersonAdmin(admin.ModelAdmin): |
|---|
| | 534 | inlines = [ |
|---|
| | 535 | FriendshipInline, |
|---|
| | 536 | ] |
|---|
| | 537 | |
|---|