InlineModelAdmin options and StackedInline

I am facing one issue when developing Django in admin, Here are my model and admin code below.

However, when I run py manage.py runserver and go to the Django admin site to add data, the PROJECT ROW CONTENT shows 3 times repetitively for the user to add the data.

TabularInline form

Where are the wrong lines in my code that result in this and Is there a way to restrict it to only one?

I try to search the solution in Django documentation but couldn’t find it out, Here this the documentation link where I thnk the answer probably in: InlineModelAdmin options¶

Model

class Project(models.Model):
    title = models.CharField(max_length=255, help_text="Name Of Project")
    slug = models.SlugField(unique=True, editable=False, null=True, blank=True) # hide from admin
    Country = CountryField(blank=True,null=False, help_text="Project's Country")
    keyword = models.TextField(max_length=1000,null=True, blank=True)
    project_type = models.ManyToManyField(Project_Type_List, related_name='project_type', help_text="Project's Type")
    thumbnail = models.ImageField(upload_to = get_project_directory_upload_path, help_text='Mandatory, will be shown as all project thumbnails', null=True, blank=False, max_length=250)
    publish = models.NullBooleanField(default=True)
    show_on_main = models.NullBooleanField(default=False, help_text='Show On Main Page')
    sorting = models.IntegerField(default=100,null=True, blank=True)
    create_at = models.DateTimeField(auto_now=False, auto_now_add=True, null=True, editable=False)
    update_at = models.DateTimeField(auto_now=True, auto_now_add=False, null=True, editable=False)

    class Meta:
        verbose_name = "Project"
        verbose_name_plural = "Project"
        ordering = ['sorting','title']

    def save(self, *args, **kwargs):
        self.slug = uuslug(self.title, instance=self)
        imp = custom_function.ImageProcessor()

        if has_changed(self, 'thumbnail' ):
            self.thumbnail = imp.image_resizer(self.thumbnail, self.thumbnail.name, "thumbnail")
        
        super(Project, self).save(*args, **kwargs)
    
    def __str__(self):
        return self.title

Admin

from .models import Project,Trend

class ProjectInline(admin.StackedInline):
    model = Project_Content # referenceto project content model

class Project_Admin(admin.ModelAdmin): # create admin in a separate form for project
    list_display = ('title','Country','sorting') # Fields to display in project admin section
    inlines = [ProjectInline]

admin.site.register(Project, Project_Admin)

You want to set InlineModelAdmin.extra, which defaults to 3. I’ve made a documentation PR so this should be more discoverable in the future: https://github.com/django/django/pull/12520

1 Like

Thanks, Appreciate for your sharing

So made a change as you said above, adding just one line under Class Project_Admin Model, Is that correct?

from .models import Project,Trend

class ProjectInline(admin.StackedInline):
    model = Project_Content # referenceto project content model

class Project_Admin(admin.ModelAdmin): # create admin in a separate form for project
    list_display = ('title','Country','sorting') # Fields to display in project admin section
    inlines = [ProjectInline]
    InlineModelAdmin.extra = 1

admin.site.register(Project, Project_Admin)