Django - 排除内联管理界面中的某些字段

发布于 2024-10-02 18:05:17 字数 37 浏览 2 评论 0原文

在 Django 管理界面中,是否可以排除内联中的某些字段?

In Django admin interface, Is to possible to exclude some of the fields in Inline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2024-10-09 18:05:17

使用 排除 你可以这样做

例如:

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']

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']
凉城 2024-10-09 18:05:17

除了Francisco Lavin的答案之外,您可以将“fields”“author”“title”一起使用,从表单中排除“short_description”字段如下所示:

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

此外,通过将“editable=False”添加到“short_description”字段,您可以从表单中排除“short_description”字段如下所示(不需要“BookInline”类):

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文