Skip to content

fix: 修复分页查询时间相同的情况下数据错乱 #1727

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

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
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: 2 additions & 2 deletions apps/dataset/serializers/dataset_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ def get_query_set(self):
query_set = QuerySet(model=get_dynamics_model(
{'temp.name': models.CharField(), 'temp.desc': models.CharField(),
"document_temp.char_length": models.IntegerField(), 'temp.create_time': models.DateTimeField(),
'temp.user_id': models.CharField(), }))
'temp.user_id': models.CharField(), 'temp.id': models.CharField()}))
if "desc" in self.data and self.data.get('desc') is not None:
query_set = query_set.filter(**{'temp.desc__icontains': self.data.get("desc")})
if "name" in self.data and self.data.get('name') is not None:
query_set = query_set.filter(**{'temp.name__icontains': self.data.get("name")})
if "select_user_id" in self.data and self.data.get('select_user_id') is not None:
query_set = query_set.filter(**{'temp.user_id__exact': self.data.get("select_user_id")})
query_set = query_set.order_by("-temp.create_time")
query_set = query_set.order_by("-temp.create_time", "temp.id")
query_set_dict['default_sql'] = query_set

query_set_dict['dataset_custom_sql'] = QuerySet(model=get_dynamics_model(

Choose a reason for hiding this comment

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

该代码段在逻辑上是正确的,但在某些地方存在一些细节需要注意:

  1. 将注释中的英文“models.CharField()”替换成中文形式,“char_field_name”。这主要是为了确保翻译和执行时保持一致。

修改后的版本如下所示:

  1. 在设置查询集对象之前添加模型信息。
    class DynamicItem(DynamicObject):
        @@ -156,8 +156,8 @@
             query_set = QuerySet(model.get_model())()

           # 如果有desc字段(包含指定字符串)则按照这个条件进行过滤
                if "desc" in self.data and self.data.get("desc") is not None:
                    query_set = query_set.filter(temp.desc_icontains=self.data["desc"])

           # 如果有username(含指定的用户名)则按照这个条件进行过滤
                if ""in self.data.values_list('', flat=True)and self.data[0] is not None:

           # 用户ID必须匹配才能返回结果

                if "id__iexact" in self.data.keys():
                     data[key]["result"] = QuerySet(user=user).filter(**{"temp.user_id": key})

这样,我们可以避免不必要的代码重复,并使整体结构更清晰。如果您需要进一步的帮助,请随时告诉我!

Expand Down
2 changes: 1 addition & 1 deletion apps/dataset/serializers/document_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def get_query_set(self):
query_set = query_set.filter(**{'is_active': self.data.get('is_active')})
if 'status' in self.data and self.data.get('status') is not None:
query_set = query_set.filter(**{'status': self.data.get('status')})
query_set = query_set.order_by('-create_time')
query_set = query_set.order_by('-create_time', 'id')
return query_set

def list(self, with_valid=False):

Choose a reason for hiding this comment

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

在提供的代码中,没有发现明显的不规范和潜在问题,但在实现list方法逻辑时可以稍微调整一下:当with_valid=True时查询set将默认排序为ID降序。如果需要按创建时间升序排列且无条件是否启用,则可以修改get_query_set返回语句。

例如:

  • 修改 get_query_set 方法:
    query_set = query_set.filter(is_active=self.data.get('is_active'), status='status')
    query_set = query_set.order_by('-create_time')
    
    
这样就可以得到满足特定条件(仅显示已激活并且指定的状态)并按照创建日期排序的list了。

对于现有的列表操作而言,这种更改几乎不会带来任何性能变化或影响程序执行效率。

Expand Down