﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18823	Clear with a M2M field with a through model	anonymous	nobody	"This ticket deals with similar issues as #15161.

When you have a M2M field with a through model where one of the foreign keys uses a to_field, the to_field is ignored by add, remove and clear. Only clear is available when using a normal through but it would be nice to fix for all of them.

To see the currently wrong behavior try something like this:

{{{
class Through(models.Model):
    ffrom = models.ForeignKey('Two', to_field='slug')
    to = models.ForeignKey('One')

class One(models.Model):
    name = models.CharField(max_length=100)

class Two(models.Model):
    slug = models.CharField(max_length=24, unique=True)
    ones = models.ManyToManyField(One, through=Through)

o = One(name='test')
t = Two(slug='test2')
Through(ffrom=t, to=o).save()
t.ones.clear()
}}}

You'll see the relationship not being cleared as the queries use the value for the pk of t instead of the value of 'slug' on t ('test2').

The fix could be as simple as changing ManyRelatedManager._pk_val to not be pk value but to lookup the correct value for the field. (line 534)"	Bug	closed	Database layer (models, ORM)	1.4	Release blocker	fixed			Accepted	1	0	0	0	0	0
