﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
7780	get returns multiple objects when model includes  a ManyToManyField in the default ordering	max@…	nobody	"Django throws a MultipleObjectsReturned error when getting a single instance of a model that includes a ManyToManyField in the default ordering.

It seems that the related table is joined with an inner join and so the resulting query has extra rows.
I traced the join to the function get_ordering of django\db\models\sql\query.py.

This simple example illustrates the issue best:


{{{
from django.db import models

class Topping(models.Model):
    name  = models.CharField(max_length = 50)

    def __unicode__(self):
        return self.name


class Pizza(models.Model):
    name = models.CharField(max_length = 50)
    toppings = models.ManyToManyField(Topping)

    def __unicode__(self):
        return self.name

    class Meta:


# test with the following 
# create some toppings
tomato = Topping(name=""Tomato"")
tomato.save()
mozzarella = Topping(name=""Mozzarella"")
mozzarella.save()

# create a pizza
pizza = Pizza(name=""Margareta"")
pizza.save()

# add the toppings
pizza.toppings.add(tomato)
pizza.toppings.add(mozzarella)

# select the pizza
p = Pizza.objects.get(pk=1)

# You should get the following error:
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""c:\python25\lib\site-packages\django\db\models\manager.py"", line 82, in get return self.get_query_set().get(*args, **kwargs)
  File ""c:\python25\lib\site-packages\django\db\models\query.py"", line 285, in get % (self.model._meta.object_name, num, kwargs))
MultipleObjectsReturned: get() returned more than one Pizza -- it returned 2! Lookup parameters were {'pk': 1}
}}}



"		closed	Uncategorized	dev		invalid			Unreviewed	0	0	0	0	0	0
