[Django]-Django – Excluding some fields in Inline Admin Interface

72👍

with exclude you can do it

ex:

class Book(models.Model):
   author = models.ForeignKey(Author)
   title = models.CharField(max_length=100)
   short_description = models.CharField(max_length=200)

class BookInline(admin.TabularInline):
    model = Book
    exclude = ['short_description']

1👍

In addition to Francisco Lavin’s answer, you can exclude "short_description" field from your form by using "fields" with "author" and "title" as shown below:

class Book(models.Model):
   author = models.ForeignKey(Author)
   title = models.CharField(max_length=100)
   short_description = models.CharField(max_length=200)

class BookInline(admin.TabularInline):
    model = Book
    fields = ['author', 'title'] # Here

And also by adding "editable=False" to "short_description" field, you can exclude "short_description" field from your form as shown below ("BookInline" class is not needed):

class Book(models.Model):
   author = models.ForeignKey(Author)
   title = models.CharField(max_length=100)             # Here
   short_description = models.CharField(max_length=200, editable=False)

Leave a comment