Assigning a QS to a M2M relation that was generated from that M2M relation clears both the QS and the relation
from django.db import models
class Topping(models.Model):
meat = models.BooleanField()
class Pizza(models.Model):
toppings = models.ManyToManyField(Topping)
>>> pizza = Pizza.objects.create()
>>> cheese = Topping.objects.create(meat=False)
>>> bacon = Topping.objects.create(meat=True)
>>> pizza.toppings = [cheese, bacon]
>>> pizza.toppings.count()
2
>>> vegs = pizza.toppings.filter(meat=False)
>>> vegs.count()
1
>>> pizza.toppings = vegs
>>> pizza.toppings.count()
0
>>> vegs.count()
0
Would expect vegs to be unchanged, and pizza.toppings to be [cheese]
The problem is the same if the vegs QS is generated with Topping.objects.filter(pizza=pizza). If it comes from somewhere not involving the relation then everything works as expected.
Change History
(12)
Triage Stage: |
Unreviewed → Accepted
|
Owner: |
changed from nobody to Aleksandra Sendecka
|
Status: |
new → assigned
|
Has patch: |
set
|
Triage Stage: |
Accepted → Ready for checkin
|
Keywords: |
sprint2013 added
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
Yes, because the queryset isn't evaluated until after the existing m2m values are cleared, meaning it evaluates to an empty queryset. Definitely a bug. Thanks for the report!