#22023: .values() followed by defer() or only() results invalid data or crash -------------------------------------+------------------------------------- Reporter: jtiai | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: master (models, ORM) | Resolution: Severity: Normal | Triage Stage: Accepted Keywords: orm | Needs documentation: 0 Has patch: 0 | Patch needs improvement: 0 Needs tests: 0 | UI/UX: 0 Easy pickings: 0 | -------------------------------------+-------------------------------------
Comment (by Lauxley): I was curious so i tracked down the issue, the iterator() method of the ValuesQuerySet doesn't use the deferred_loading attribute of the query, even though the docstring of Query.deferred_to_data says that : "This [self.deferred_loading] is used to compute the columns to select from the database and also by the QuerySet class to work out which fields are being initialised on each model." So a quick patch (django 1.4.5) {{{#!python @@ -956,7 +956,14 @@ def iterator(self): # Purge any extra columns that haven't been explicitly asked for extra_names = self.query.extra_select.keys() - field_names = self.field_names + deferred_fields, defer = self.query.deferred_loading + if deferred_fields: + if not defer: + field_names = list(deferred_fields) + else: + field_names = list(set(self.field_names) - deferred_fields) + else: + field_names = self.field_names aggregate_names = self.query.aggregate_select.keys() names = extra_names + field_names + aggregate_names }}} This is probably not the 'right' way to do it, only an hint. I strongly feel that this should not be computed in the iterator method, i'm not even sure why the QuerySet class would need to do it itself, but i'm probably missing something. Note: .values('field_a').only('field_b') and .values('field_a').defer('field_a') will still raise a DatabaseError, but i think it's fine in this case. -- Ticket URL: <https://code.djangoproject.com/ticket/22023#comment:2> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/063.064a6cbe18a181f629ae63699e8938e5%40djangoproject.com. For more options, visit https://groups.google.com/groups/opt_out.