Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/tools/serializers/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ def delete(self):

def one(self):
self.is_one_valid(raise_exception=True)
tool = QuerySet(Tool).filter(id=self.data.get('id')).first()
tool = QuerySet(Tool).filter(id=self.data.get('id')).select_related('user').first()
nick_name = tool.user.nick_name if tool and tool.user else None
if tool.init_params:
tool.init_params = json.loads(rsa_long_decrypt(tool.init_params))
if tool.init_field_list:
Expand All @@ -528,6 +529,7 @@ def one(self):
return {
**ToolModelSerializer(tool).data,
'init_params': tool.init_params if tool.init_params else {},
'nick_name': nick_name
}

def export(self):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has a few minor improvements and optimizations that can be made:

  1. Add type hints for better readability:
def delete(self) -> None:
    ...

def one(self) -> dict:
    ...

def export(self) -> list[dict]:
    ...
  1. Use f-string formatting instead of string concatenation:
return {"a": "b", **data}
# becomes...
return {"a": b"value"}

These small changes make the code more readable, maintainable, and adhere to modern Python coding standards.

However, please note that if Tool is an expensive model with many fields, selecting only related objects (using .select_related('user')) might not save any significant resources due to database constraints and indexing. Always consider performance implications in specific use cases.

Expand Down
Loading