I have 2 models:

class District(Model):
  pass

class Voter(Model):
   districts = ManyToManyField(District)

Sometimes I want to reset the m2m list:

voter.districts = other_districts

I noticed that this uses ManyRelatedManager._clear_items, which, among
other things, boils down to:

voter.districts.through._default_manager.filter(voter_id=voter.pk).delete()

I would have expected something along the lines of :

DELETE FROM `api_voter_districts` WHERE `voter_id` = 1

But instead it's 2 queries:

SELECT `api_voter_districts`.`id`, `api_voter_districts`.`voter_id`,
`api_voter_districts`.`district_id` FROM `api_voter_districts` WHERE
`api_voter_districts`.`voter_id` = 1

DELETE FROM `api_voter_districts` WHERE `id` IN (2, 3, 4, 5, 6,...)

Was it intentional to take 2 queries?  That is, is there a use case
where avoiding the single DELETE query is preferable?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" 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-developers?hl=en.

Reply via email to