

def validate(self, value, model_instance):
    if self.rel.parent_link:
        return
    super(ForeignKey, self).validate(value, model_instance)
    if (value is None):
        return
    using = router.db_for_read(model_instance.__class__, instance=model_instance)
    qs = self.rel.to._default_manager.using(using).filter(**{
        self.rel.field_name: value,
    })
    qs = qs.complex_filter(self.rel.limit_choices_to)
    if (not qs.exists()):
        raise exceptions.ValidationError((self.error_messages['invalid'] % {
            'model': self.rel.to._meta.verbose_name,
            'pk': value,
        }))

dataset/ETHPy150Open AppScale/appscale/AppServer/lib/django-1.5/django/db/models/fields/related.py ForeignKey.validate/original