#22268: values_list() on a ManyToManyField returns extra None's when iterated over. -------------------------------------+------------------------------------- Reporter: k_sze | Owner: Type: Bug | anubhav9042 Component: Database layer | Status: assigned (models, ORM) | Version: master Severity: Normal | Resolution: Keywords: orm, values_list, | Triage Stage: Accepted ManyToManyField | Needs documentation: 0 Has patch: 0 | Patch needs improvement: 0 Needs tests: 0 | UI/UX: 0 Easy pickings: 0 | -------------------------------------+-------------------------------------
Comment (by k_sze): To demonstrate how bad things become once you have multiple M2M fields, I created a new `Language` model and add it as a M2M field in the Class model: {{{ from django.db import models # other Model definitions skipped ... class Class(models.Model): school = models.ForeignKey(School) name = models.CharField(unique=True, max_length=255) students = models.ManyToManyField('Student') available_in_languages = models.ManyToManyField('Language') class Language(models.Model): name = models.CharField(max_length=255) }}} Now I create new languages and add them to the Discrete Mathematics class (which now has 2 students, if you recall): {{{ # French >>> french = Language(name='French') >>> french.save() # English >>> english = Language(name='English') >>> english.save() # Add them to Discrete Mathematics class discrete_math = Class.objects.get(name='Discrete Mathematics') discrete_math.available_in_languages.add(french) discrete_math.available_in_languages.add(english) }}} Now I make a `values()` query: {{{ >>> Class.objects.values('available_in_languages', 'students') [{'students': 1, 'available_in_languages': None}, {'students': None, 'available_in_languages': None}, {'students': 1, 'available_in_languages': 2}, {'students': 2, 'available_in_languages': 2}, {'students': 1, 'available_in_languages': 1}, {'students': 2, 'available_in_languages': 1}] }}} That's 6 dictionaries now! Similarly, with `values_list()`: {{{ >>> Class.objects.values_list('available_in_languages', 'students') [(None, 1), (None, None), (2, 1), (2, 2), (1, 1), (1, 2)] }}} 6 tuples even though we only have 3 Class objects. -- Ticket URL: <https://code.djangoproject.com/ticket/22268#comment:16> 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.30c1be9120201a5b3bec9e7987c1de89%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.