def apply_limit_choices_to_form_field(formfield):
    """Apply limit_choices_to to the formfield's query if needed."""
    if hasattr(formfield, 'queryset') and hasattr(formfield,
        'get_limit_choices_to'):
        limit_choices_to = formfield.get_limit_choices_to()
        if limit_choices_to is not None:
            formfield.queryset = formfield.queryset.filter(*limit_choices_to)
