-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Paragraph list unsorted #2411
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -663,6 +663,7 @@ def get_query_set(self): | |
**{'title__icontains': self.data.get('title')}) | ||
if 'content' in self.data: | ||
query_set = query_set.filter(**{'content__icontains': self.data.get('content')}) | ||
query_set.order_by('-create_time', 'id') | ||
return query_set | ||
|
||
def list(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one minor issue with the provided code. In Django's ORM, Additionally, adding def get_queryset(self):
query_set = super().get_queryset()
if 'title' in self.data:
query_set = query_set.filter(title__icontains=self.data.get('title'))
if 'content' in self.data:
query_set = query_set.filter(content__icontains=self.data.get('content'))
# Optional but recommended: Order by creation time first so older posts appear first.
if 'sortby_date' in self.request.GET and self.request.GET['sortby_date'].lower() == 'asc':
query_set = query_set.order_by('create_time') # Default ascending order, comment this out if not needed
else:
query_set = query_set.order_by('-create_time')
return query_set This ensures proper ordering and possibly adds an extra layer of flexibility if other sorting options are added later. Additionally, it checks for a specific sort request parameter to allow either asc or desc order. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some potential issues and optimizations:
Code Repeats: The code for
one
andbatch_save
methods is nearly identical, but different in their return values (single vs multiple results). Consider refactoring to reduce redundancy.Order By Clause: Adding an order by clause when calling
native_search
can sometimes slow down the query, especially if not properly indexed. It's better to handle ordering after retrieving data if performance becomes a concern.Parameter Naming: In
edit
method, it would be more descriptive to use parameter names likeinstance_dict
.Comments and Docstrings: The comments in your current code are quite short and don't cover all functionalities well. Consider expanding them to provide clear explanations of each part of the method.
Error Handling: Your methods do not include explicit error handling around database operations or custom SQL queries. This could lead to silent failures that might be difficult to diagnose later.
Here’s a revised version with some suggested improvements:
This rework addresses some areas of inefficiency and improves readability. Always ensure you have proper unit tests covering these changes before deployment.