Opened 8 years ago
Last modified 18 months ago
#26706 closed Bug
ManyToMany clear doesn't clear the items from the object but deletes from db — at Version 2
Reported by: | Sharat M R | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | manytomany m2m clear |
Cc: | mattdentremont@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
class Media(models.Model): name = models.CharField(max_length=100) class Tag(models.Model): tag = models.CharField(max_length=100) medias = models.ManyToManyField(Media) def __unicode__(self): return self.tag
Assume tag object below already has three items of Media model in medias which are saved in db. [m1, m2, m3].
tag = Tag.objects.get(id=1) tag.medias.clear() print(tag.medias.all())
The above code prints all the three [m1, m2, m3]. Fetching the tag again using get and printing givens an empty list "[ ]"
Failing Testcase
class Test(TestCase): def test(self): tag = Tag.objects.create(tag='Tag') tag.medias.create(name='A') tag.medias.create(name='B') tag.medias.create(name='C') tag = Tag.objects.prefetch_related('medias').get(id=1) tag.medias.clear() print(tag.medias.all())
I guess its related to prefetch_related function.
Change History (2)
comment:1 by , 8 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
comment:2 by , 8 years ago
Description: | modified (diff) |
---|---|
Resolution: | worksforme |
Status: | closed → new |
class Test(TestCase): def test(self): tag = Tag.objects.create(tag='Tag') tag.medias.create(name='A') tag.medias.create(name='B') tag.medias.create(name='C') tag = Tag.objects.prefetch_related('medias').get(id=1) tag.medias.clear() print(tag.medias.all())
This test print all the three medias for me
Note:
See TracTickets
for help on using tickets.
I can't reproduce that. Here's my complete test case:
Also, Django's test suite tests the behavior.
Please reopen if you can provide a failing test case.