diff options
| author | Anssi Kääriäinen <akaariai@gmail.com> | 2013-02-10 21:49:03 +0200 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-02-10 21:54:48 +0200 |
| commit | 77f6eb2a6099fc06d01c77df2374e0528351822b (patch) | |
| tree | 10df142cf4083725685dbbd61823b32ec7b18660 | |
| parent | 44afe4460218c1207ca9d7b1495e3f60cb06d898 (diff) | |
[1.5.x] Made custom m2m fields without through easier to use
The change in f105fbe52b21da206bfbaedf0e92326667d7b2d4 made through=None
m2m fields fail in cases where they worked before. It isn't possible to
create such fields using public APIs. The fix is trivial, so it seems
worth adding it.
This is not a backport from master. Master has gotten enough other
changes to related fields internal API that this fix alone isn't enough
to do any good.
| -rw-r--r-- | django/db/models/fields/related.py | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index ead1d49b57..387d447ff6 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -591,12 +591,14 @@ def create_many_related_manager(superclass, rel): "a many-to-many relationship can be used." % instance.__class__.__name__) - def _get_fk_val(self, obj, field_name): """ Returns the correct value for this relationship's foreign key. This might be something else than pk value when to_field is used. """ + if not self.through: + # Make custom m2m fields with no through model defined usable. + return obj.pk fk = self.through._meta.get_field(field_name) if fk.rel.field_name and fk.rel.field_name != fk.rel.to._meta.pk.attname: attname = fk.rel.get_related_field().get_attname() |
