def validate(self, value, model_instance):
     if self.has_choices_callback:
         if self.rel.parent_link:
             return
         if value is None:
             return
         
         data = model_to_dict(model_instance)
         for field in model_instance._meta.fields:
             try:
                 data[field.name] = getattr(model_instance, field.name)
             except field.rel.to.DoesNotExist:
                 pass
         if model_instance.id:
             for m2m in model_instance._meta.many_to_many:
                 data[m2m.name] = getattr(model_instance, m2m.name).all()
         
         qs = self.rel.to._default_manager.filter(**{self.rel.field_name:value})
         qs = qs.complex_filter(self.rel.limit_choices_to)
         
         dcqs = self._invoke_choices_callback(model_instance, qs, data)
         # If a tuple is provided we must build
         # a new Queryset by combining group's ones
         if isinstance(dcqs, tuple):
             qs = qs and unionize_querysets(q[1] for q in dcqs)
         else:
             qs = dcqs
         
         if not qs.exists():
             raise exceptions.ValidationError(self.error_messages['invalid'] % {
                 'model': self.rel.to._meta.verbose_name, 'pk': value})
     else:
         super(DynamicChoicesForeignKey, self).validate(value, model_instance)
 def _set_queryset(self, queryset):
     self._original_queryset = queryset
     self._groups = None
     if self._instance and isinstance(queryset, DynamicChoicesQueryset):
         queryset = queryset.filter_for_instance(self._instance, self._data)
         if isinstance(queryset, tuple):
             self._groups = queryset
             queryset = unionize_querysets(q[1] for q in queryset)
     self._queryset = queryset
     self.widget.choices = self.choices