I have ModelFormone that contains some ForeignKey, for example, for a User object, but it can be for any other model. I have a unit test class for this form, but when I try to pass its data, I get an error Select a valid choice. That choice is not one of the available choices. The test looks like this:
class Monkey(Model):
user = models.ForeignKey(User)
...
class MyForm(ModelForm):
class Meta:
model = Monkey
fields = ['user', ...]
def test_my_form_with_a_user(self):
...
data = {'user': User.objects.get(pk=1), ... }
data = {'user': [u'1'], ... }
data = {'user': [u'JaneDoe'], ... }
form = MyForm(data, ...)
self.assertTrue(form.is_valid(), form.errors)
...
I tried any number of permutations for user, but I get the same error.
What am I missing?
source
share