#26706: ManyToMany clear doesn't clear the items from the object but deletes 
from
db
-------------------------------------+-------------------------------------
     Reporter:  cosmosgenius         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  manytomany, m2m,     |             Triage Stage:
  clear                              |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by cosmosgenius):

 * status:  closed => new
 * resolution:  worksforme =>


Old description:

> {{{
>
> 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 "[ ]"

New description:

 {{{

 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.

--

Comment:

 {{{
 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())
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26706#comment:2>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.20ac0a493db239631a6013f59d92b93f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to