| 1389 | |
| 1390 | # ModelChoiceFieldField & ModelMultipleChoiceField ############################### |
| 1391 | |
| 1392 | # Create choices for the model choice field tests below. |
| 1393 | >>> from regressiontests.forms.models import ChoiceModel |
| 1394 | >>> ChoiceModel.objects.create(name='x') |
| 1395 | <ChoiceModel: ChoiceModel object> |
| 1396 | |
| 1397 | >>> from regressiontests.forms.models import ChoiceModelCharPK |
| 1398 | >>> ChoiceModelCharPK.objects.create(name='x') |
| 1399 | <ChoiceModelCharPK: ChoiceModelCharPK object> |
| 1400 | |
| 1401 | # ModelChoiceField ############################################################ |
| 1402 | |
| 1403 | >>> f = ModelChoiceField(queryset=ChoiceModel.objects.all()) |
| 1404 | >>> pk = ChoiceModel.objects.all()[0].pk |
| 1405 | >>> f.clean(pk) |
| 1406 | <ChoiceModel: ChoiceModel object> |
| 1407 | >>> f.clean('NotAnInteger') |
| 1408 | Traceback (most recent call last): |
| 1409 | ... |
| 1410 | ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] |
| 1411 | |
| 1412 | >>> f = ModelChoiceField(queryset=ChoiceModelCharPK.objects.all()) |
| 1413 | >>> pk = ChoiceModelCharPK.objects.all()[0].pk |
| 1414 | >>> f.clean(pk) |
| 1415 | <ChoiceModelCharPK: ChoiceModelCharPK object> |
| 1416 | >>> f.clean('Z') |
| 1417 | Traceback (most recent call last): |
| 1418 | ... |
| 1419 | ValidationError: [u'Select a valid choice. That choice is not one of the available choices.'] |
| 1420 | |
| 1421 | >>> f = ModelMultipleChoiceField(queryset=ChoiceModel.objects.all()) |
| 1422 | >>> pk = ChoiceModel.objects.all()[0].pk |
| 1423 | >>> f.clean([pk]) |
| 1424 | [<ChoiceModel: ChoiceModel object>] |
| 1425 | >>> f.clean(['NotAnInteger']) |
| 1426 | Traceback (most recent call last): |
| 1427 | ... |
| 1428 | ValidationError: [u'Select a valid choice. NotAnInteger is not one of the available choices.'] |
| 1429 | |
| 1430 | >>> f = ModelMultipleChoiceField(queryset=ChoiceModelCharPK.objects.all()) |
| 1431 | >>> pk = ChoiceModelCharPK.objects.all()[0].pk |
| 1432 | >>> f.clean([pk]) |
| 1433 | [<ChoiceModelCharPK: ChoiceModelCharPK object>] |
| 1434 | >>> f.clean(['Z']) |
| 1435 | Traceback (most recent call last): |
| 1436 | ... |
| 1437 | ValidationError: [u'Select a valid choice. Z is not one of the available choices.'] |