简体   繁体   English

.only() 方法的问题,传递给分页/序列化 --- 所有字段都被返回,而不是 only() 中指定的字段

[英]Problem with .only() method, passing to Pagination / Serialization --- all fields are getting returned instead of the ones specified in only()

I am trying load some data into datatables.我正在尝试将一些数据加载到数据表中。 I am trying to specify columns in the model.objects query by using.only() --- at first glance at the resulting QuerySet, it does in fact look like the mySQL query is only asking for those columns.我试图通过 using.only() 指定 model.objects 查询中的列 --- 乍一看生成的 QuerySet,它实际上看起来像 mySQL 查询只要求这些列。

However, When I try to pass the QuerySet into Paginator, and/or a Serializer, the result has ALL columns in it.但是,当我尝试将 QuerySet 传递给 Paginator 和/或序列化器时,结果中包含所有列。

I cannot use.values_list() because that does not return the nested objects that I need to have serialized as part of my specific column ask.我不能 use.values_list() 因为它不会返回我需要序列化的嵌套对象,作为我的特定列询问的一部分。 I am not sure what is happening to my.only()我不确定 my.only() 发生了什么

db_result_object = model.objects.prefetch_related().filter(qs).order_by(asc+sort_by).only(*columns_to_return)

    paginated_results = Paginator(db_result_object,results_per_page)
    serialized_results = serializer(paginated_results.object_list,many=True)
    paginated_results.object_list = serialized_results.data

    return paginated_results

This one has tripped me up too.这一个也让我绊倒了。 In Django, calling only() doesn't return data equivalent to a SQL statement like this:在 Django 中,调用only()不会返回与 SQL 语句等效的数据,如下所示:

SELECT col_to_return_1, ... col_to_return_n
FROM appname_model

The reason it doesn't do it like this is because Django returns data to you not when you construct the QuerySet, but when you first access data from that QuerySet ( see lazy QuerySets ).它不这样做的原因是因为 Django 不是在您构造 QuerySet 时向您返回数据,而是在您首次访问该 QuerySet 中的数据请参阅惰性 QuerySets )。

In the case of only() (a specific example of what is called a deferred field) you still get all of the fields like you normally would, but the difference is that it isn't completely loaded in from the database immediately.only()的情况下(称为延迟字段的特定示例),您仍然可以像往常一样获得所有字段,但不同之处在于它不会立即从数据库中完全加载。 When you access the data, it will only load the fields included in the only statement.当您访问数据时,它只会加载唯一语句中包含的字段。 Some useful docs here . 这里有一些有用的文档。

My recommendation would be to write your Serializer so that it is only taking care of the one specific filed, likely using a SerializerMethodField with another serializer to serialize your related fields.我的建议是编写您的序列化程序,以便它只处理一个特定的字段,可能使用带有另一个序列化程序的 SerializerMethodField 来序列化您的相关字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django-filter 显示所有字段,而不仅仅是指定的字段 - django-filter shows all fields instead of just the ones specified Django表单如何仅初始化不需要的字段而不是传递所有必需的字段 - Django forms how to initialze only not required field instead of passing all required fields 如何在Django中获取相关模型的所有字段而不是仅id - how to get all fields of related model in django instead of only id 如何通过从左表中获取所有行并仅在右表中匹配来连接 2 个表 - How to join 2 tables with getting all rows from left table and only matching ones in right 仅使用queryset方法时,DRF响应会返回所有字段 - DRF Response returning all fields when using queryset method only Django仅从列表中获取一个字符串,而不是全部 - Django Getting Only One String from List Instead of All Django REST 框架深度仅适用于指定字段 - Django REST Framework Depth Only For Specified Fields django TemplateView使用分页并仅获得1个结果 - django TemplateView use pagination and getting only 1 result 筛选所有用户以在管理界面中仅显示活动用户 - Filter all Users to show only active ones in admin interface 使用 get_object_or_404 只获取一个字段而不是 django rest 框架中模型中指定的所有字段? - Use get_object_or_404 to get only one field rather than all fields specified in model in django rest framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM