#7780: get returns multiple objects when model includes a ManyToManyField in
the
default ordering
----------------------------+-----------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: nobody
Status: new | Milestone:
Component: Uncategorized | Version: SVN
Keywords: | Stage: Unreviewed
Has_patch: 0 |
----------------------------+-----------------------------------------------
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}
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/7780>
Django Code <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---