Django admin inline auto creates multiple fields on admin panel

Question:

I have a TicketMessage model that contains a foreign key to Ticket.
I wanted to have my TicketMessage message field appear on my Ticket admin panel as an inline.

I did succeeded in doing so however as shown in the image below for some reason by default it creates three message fields instead of one.

Would like to know how to make it so it shows only one message by default and the reason why this happens in the first place.

TicketMessage model

from django.db import models
from django.utils.translation import gettext as _

from painless.models import TimeStampMixin

class TicketMessage(TimeStampMixin):

    class Meta:
        verbose_name = _("Ticket Message"),
        verbose_name_plural = ("Ticket Messages")

  
    ticket = models.ForeignKey(
        "Ticket",
        default=False,
        on_delete=models.PROTECT,
        related_name="ticket_message",
        help_text="Lorem ipsum"
    )

    message = models.TextField(
        _("message"),
        max_length=500,
        help_text="Lorem ipsum",
    )


    def __str__(self):
        return f"Ticket subject: {self.ticket}"

    def __repr__(self):
        return f"Ticket subject: {self.ticket}"


Ticket admin

from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from desk.models import (
    Ticket,
    TicketMessage
)


class TicketMessageInline(admin.TabularInline):
    model = TicketMessage


@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):

    list_display = (
        'subject',
        'status_choices',
        'priority_choices',
        'is_read',
        'is_important',
        'is_archive',
    )

    list_filter = (
        'is_read',
        'is_important',
        'is_archive',
        'status_choices',
    )

    inlines = (
        TicketMessageInline,
    )

    filter_horizontal = (
        "tags",
    )

    
    fieldsets = (
        (_("Primary Informations"), {
            'fields': (
                'subject',
                'message',
                'status_choices',
                'priority_choices',
            )
        }),

        (_("Options"), {
            'fields': (
                'is_important',
                'is_archive',
                'is_read',
                'is_duplicated',
            )
        }),

        
        (_("Others"), {
            'fields': (
                'department',
                'resolved_at',
            )
        }),

        
        (_("Tags"), {
            'fields': (
                'tags',
            )
        }),
    )

bug image
Bug imag

Solution

As @Mojtaba pointed out, adding extra = 1 fixes the issue

from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from desk.models import (
    Ticket,
    TicketMessage
)


class TicketMessageInline(admin.TabularInline):
    model = TicketMessage
    extra = 1


@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):

    list_display = (
        'subject',
        'status_choices',
        'priority_choices',
        'is_read',
        'is_important',
        'is_archive',
    )

    list_filter = (
        'is_read',
        'is_important',
        'is_archive',
        'status_choices',
    )

    inlines = (
        TicketMessageInline,
    )

    filter_horizontal = (
        "tags",
    )


    fieldsets = (
        (_("Primary Informations"), {
            'fields': (
                'subject',
                'message',
                'status_choices',
                'priority_choices',
            )
        }),

        (_("Options"), {
            'fields': (
                'is_important',
                'is_archive',
                'is_read',
                'is_duplicated',
            )
        }),


        (_("Others"), {
            'fields': (
                'department',
                'resolved_at',
            )
        }),


        (_("Tags"), {
            'fields': (
                'tags',
            )
        }),
    )

Asked By: King of Cards

||

Answers:

in your inline use

extra = 1

this variable show how many empty inlines should be shown in admin panel.

Answered By: Mojtaba
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.