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

Conversation

shaohuzhang1
Copy link
Contributor

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

Copy link

f2c-ci-robot bot commented Dec 2, 2024

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@shaohuzhang1 shaohuzhang1 merged commit 94c5219 into main Dec 2, 2024
4 checks passed
Copy link

f2c-ci-robot bot commented Dec 2, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_document_page branch December 2, 2024 03:06
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})

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

@@ -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了。

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants