Hi Malcolm, Thanks for the tip! It works like I wanted it to.
Thanks! On Jan 20, 4:44 pm, Malcolm Tredinnick <malc...@pointy-stick.com> wrote: > On Mon, 2009-01-19 at 22:38 -0800, I.A wrote: > > Hi guys, > > I apologize for the confusing title. > > > I'm still confused on using the models in a m2m relationship. Suppose > > I have the models below from the tutorials on the django site: > > > class Author(models.Model): > > name = models.CharField(max_length=50) > > email = models.EmailField() > > > def __unicode__(self): > > return self.name > > > class Entry(models.Model): > > blog = models.ForeignKey(Blog) > > headline = models.CharField(max_length=255) > > body_text = models.TextField() > > pub_date = models.DateTimeField() > > authors = models.ManyToManyField(Author) > > > If I have the Author that I want I can get all the entries related to > > this Author by doing > > > query_set = author.entry_set.all() > > > If I do query_set.delete() at this point, I know it will delete all > > the Entry data that relates to the particular Author, which will also > > delete all relationships the other Authors had with the Entries in the > > query_set. > > > How do I then delete just the relationship of that particular Author > > to those Entries without effecting other Author's relationship (i.e > > deleting the data in the intermediary table only). If the are no other > > Authors relating to these Entries, it would be ok then to delete those > > Entries too. > > There's a clear() method that works on reverse relations. It's only > documented ([1]) as being for ForeignKey fields, but I just tested it > (after finding it by browsing the source) and it appears to work on > ManyToManyFields as well. Thus, > > author.entry_set.clear() > > does what you're after. It won't remove any Entry objects that > subsequently end up with zero attached authors, as noted in the > documentation. You'll have to do an extra query for that: > > Entry.objects.filter(authors=None).delete() > > [1]http://docs.djangoproject.com/en/dev/ref/models/relations/ > > Regards, > Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---