Bayko
Bayko

Reputation: 1434

Django admin inline "add more item" button not working

Trying to learn stacked inlines in Django. Have a very basic setup

For admin.py

from django.contrib import admin

from .models import Picture, Review


class ReviewInline(admin.StackedInline):
    model = Review
    save_on_top = True
    fields = ["reviewer"]

#@admin.register(Picture)
class PictureAdmin(admin.ModelAdmin):
    save_on_top = True
    fields = ["painter"]
    inlines = [ReviewInline,]


admin.site.register(Review)
admin.site.register(Picture, PictureAdmin)

For models.py

from django.db import models

class Picture(models.Model):
    painter = models.CharField(("painter"), max_length=255)
    def __str__(self):
        return self.painter


class Review(models.Model):
    picture = models.ForeignKey(Picture, on_delete=models.CASCADE)
    reviewer = models.CharField( max_length=255)
    extra = 0
    def __str__(self):
        return self.reviewer

Here is a picture

As can be seen there is no "add more item" button. I think this might be a JS issue but am not sure(I do have JS enabled in browser)

Anyone has any idea?

Upvotes: 0

Views: 3166

Answers (2)

Bayko
Bayko

Reputation: 1434

Clearing my Google Chrome cache solved it! I got a clue after realizing that the example worked in Microsoft Edge.

Upvotes: 0

Stefan Collier
Stefan Collier

Reputation: 4682

I beleive you have the extra=0 in the wrong class, it should be in the Inline not the Model...

Remove extra=0 from the model

class Review(models.Model):
    picture = models.ForeignKey(Picture, on_delete=models.CASCADE)
    reviewer = models.CharField( max_length=255)

    # extra = 0     <---- remove this

    def __str__(self):
        return self.reviewer

Add it to the Inline:

class ReviewInline(admin.StackedInline):
    model = Review
    save_on_top = True
    extra = 0
    fields = ["reviewer"]

Justification comes from this snippet from this example:

@admin.register(Painter)
class PainterAdmin(admin.ModelAdmin):
    save_on_top = True
    fields = ["name"]
    inlines = [PictureInline]


class ReviewInline(admin.StackedInline):
    model = Review
    extra = 0
    fields = ["reviewer", "comment"]

Edit: Second thought you may also want to get rid of the save_on_top from the inline as well?

Upvotes: 1

Related Questions