TooManyFieldsSent Raised when saving admin.ModelAdmin with TabularInline even with unchanged fields
Whenever I try to save without editing any fields in Author in the admin page, I get a TooManyFields
error. I am aware that I can simply set this in settings.py.
DATA_UPLOAD_MAX_NUMBER_FIELDS = None
# or DATA_UPLOAD_MAX_NUMBER_FIELDS = some_large_number
However, not setting a limit would open up an attack vector and would pose security risks. I'd like to avoid this approach as much as possible.
My admin model AuthorAdmin
has a TabularInline
called PostInline
.
admin.py
class AuthorAdmin(admin.ModelAdmin)
inlines = [PostInline]
class PostInline(admin.TabularInline)
model = Post
Models
class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return str(self.name)
class Post(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
def __str__(self):
return str(self.title)
The reason this error is being raised is that an Author
can have multiple posts. Whenever this Author
is saved (without any field change), it sends out a POST
request which includes the post_id
and the post__series_id
based on the number of posts under that same author. This can easily exceed the default limit set by Django
which is set to 1000
.
Is there a way to not include unchanged fields in the POST
request upon save? Or a better approach to solve this issue without having to resort to updating DATA_UPLOAD_MAX_NUMBER_FIELDS
?
Could you try
class PostInline(admin.TabularInline)
model = Post
def has_change_permission(self, request, obj=None):
return False
Can't test it right now, but if you can't edit the item, maybe it's not saved either.