diff options
author | Simon Charette <charette.s@gmail.com> | 2014-11-16 17:21:33 +0100 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-11-21 16:02:31 -0500 |
commit | c7087bc777a078f71a0705d9d0eba51d2679a206 (patch) | |
tree | 856a65f2c5d7d0be7af73590233bb628ab1257fa /tests/m2m_through/tests.py | |
parent | a7c3d0f288a66d327e71476e9a9bfcc791602b8c (diff) | |
download | django-c7087bc777a078f71a0705d9d0eba51d2679a206.tar.gz |
Fixed #23862 -- Made ManyToManyRel.get_related_field() respect to_field.
Diffstat (limited to 'tests/m2m_through/tests.py')
-rw-r--r-- | tests/m2m_through/tests.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/tests/m2m_through/tests.py b/tests/m2m_through/tests.py index 25ec074d91..80c1e6c393 100644 --- a/tests/m2m_through/tests.py +++ b/tests/m2m_through/tests.py @@ -6,7 +6,8 @@ from operator import attrgetter from django.test import TestCase from .models import (Person, Group, Membership, CustomMembership, - PersonSelfRefM2M, Friendship, Event, Invitation, Employee, Relationship) + PersonSelfRefM2M, Friendship, Event, Invitation, Employee, Relationship, + Ingredient, Recipe, RecipeIngredient) class M2mThroughTests(TestCase): @@ -426,3 +427,30 @@ class M2mThroughReferentialTests(TestCase): ['peter', 'mary', 'harry'], attrgetter('name') ) + + +class M2mThroughToFieldsTests(TestCase): + def setUp(self): + self.pea = Ingredient.objects.create(iname='pea') + self.potato = Ingredient.objects.create(iname='potato') + self.tomato = Ingredient.objects.create(iname='tomato') + self.curry = Recipe.objects.create(rname='curry') + RecipeIngredient.objects.create(recipe=self.curry, ingredient=self.potato) + RecipeIngredient.objects.create(recipe=self.curry, ingredient=self.pea) + RecipeIngredient.objects.create(recipe=self.curry, ingredient=self.tomato) + + def test_retrieval(self): + # Forward retrieval + self.assertQuerysetEqual( + self.curry.ingredients.all(), + [self.pea, self.potato, self.tomato], lambda x: x + ) + # Backward retrieval + self.assertEqual(self.tomato.recipes.get(), self.curry) + + def test_choices(self): + field = Recipe._meta.get_field('ingredients') + self.assertEqual( + [choice[0] for choice in field.get_choices(include_blank=False)], + ['pea', 'potato', 'tomato'] + ) |