Skip to content

Commit

Permalink
Disregard non-editable fields while saving model
Browse files Browse the repository at this point in the history
  • Loading branch information
bhch committed Mar 14, 2023
1 parent 44305f9 commit eab7909
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tornadmin/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ def get_form(self, request_handler):
async def save_model(self, request_handler, form, obj=None):
raise NotImplementedError('Implement in subclass')

def field_is_editable(self, field):
"""Returns True if this field is editable. Else, returns False.
This method is a shorthand for checking the ``fields``,
``exclude`` and ``readonly_fields`` lists.
"""
if field in self.exclude:
return False

if field in self.readonly_fields:
return False

if self.fields and field in self.fields:
return False

return True

def get_absolute_url(self, request_handler, obj):
return None

Expand Down
4 changes: 4 additions & 0 deletions tornadmin/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ async def post(self, app_slug, model_slug,):
data = MultiDict()

for field_name in form_class._fields:
if not admin.field_is_editable(field_name):
continue
value = self.get_body_arguments(field_name, None)
if value:
data.extend(list(zip([field_name] * len(value), value)))
Expand Down Expand Up @@ -345,6 +347,8 @@ async def post(self, app_slug, model_slug, id):
data = MultiDict()

for field_name in form_class._fields:
if not admin.field_is_editable(field_name):
continue
value = self.get_body_arguments(field_name, None)
if value:
data.extend(list(zip([field_name] * len(value), value)))
Expand Down

0 comments on commit eab7909

Please sign in to comment.