Code

Ticket #4046: formfield_patch.3.diff

File formfield_patch.3.diff, 1.5 KB (added by webograph <webograph@…>, 7 years ago)

svn upped to 5281 (didn't merge cleanly any more), i18n'ed exception. no changes as suggested by paul 05/16/07 yet.

Line 
1Index: django/newforms/models.py
2===================================================================
3--- django/newforms/models.py   (revision 5281)
4+++ django/newforms/models.py   (working copy)
5@@ -169,6 +169,8 @@
6             return None
7         try:
8             value = self.queryset.model._default_manager.get(pk=value)
9+            if value not in self.queryset:
10+                   raise self.queryset.model.DoesNotExist(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
11         except self.queryset.model.DoesNotExist:
12             raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
13         return value
14Index: django/db/models/fields/related.py
15===================================================================
16--- django/db/models/fields/related.py  (revision 5281)
17+++ django/db/models/fields/related.py  (working copy)
18@@ -553,7 +553,12 @@
19         setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
20 
21     def formfield(self, **kwargs):
22-        defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.all()}
23+        defaults = {'form_class': forms.ModelChoiceField}
24+       if hasattr(self.rel.limit_choices_to, 'get_sql'):
25+             defaults['queryset']=self.rel.to._default_manager.filter(self.rel.limit_choices_to)
26+       else:
27+             defaults['queryset']=self.rel.to._default_manager.filter(**self.rel.limit_choices_to)
28+
29         defaults.update(kwargs)
30         return super(ForeignKey, self).formfield(**defaults)
31