0

I wrote this django 1.7.7 code:

class Color(models.Model):                                                          
    color = models.CharField(max_length=20, primary_key=True)                    
    def __unicode__(self):                                                          
        return str(self.color)                                                      

class Pen(models.Model):                                                            
    label = models.CharField(max_length=20, primary_key=True)                       
    color = models.ForeignKey('Color')

class PenAdmin(admin.ModelAdmin):                                                   
    pass                                                                         

class PenInline(admin.TabularInline):                                               
    model = Pen                                                                     

class ColorAdmin(admin.ModelAdmin):                                                 
    inlines = [PenInline,]                                                          

admin.site.register(Pen, PenAdmin)                                                  
admin.site.register(Color, ColorAdmin)

I want to know why when I click on the add color button in the admin page, it also shows 3 pen fields as in the image below and how to remove them from that specific dialog. I've tried both TabularInline and StackedInline and they look identical.

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
max
  • 9,708
  • 15
  • 89
  • 144

1 Answers1

1

InlineModelAdmin extra's default value is 3, so you can simply change it extra = 0 in PenInline declaration:

class PenInline(admin.TabularInline):                                               
    model = Pen
    extra = 0

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.extra

Or look at https://stackoverflow.com/a/2228821/3033586

Or remove ColorAdmin declaration and change last line to admin.site.register(Color)

Community
  • 1
  • 1
madzohan
  • 11,488
  • 9
  • 40
  • 67