Re: Error when Rendering form with ModelChoiceField
On Wed, Jun 3, 2009 at 5:12 PM, justind wrote: > > Hello, > > I'm using the following to provide a select box with filtered choices: > > class AssetForm(ModelForm): >"""Asset form takes a project id. It only allows workstreams > attached to >this project name.""" >workstreams = forms.ModelChoiceField(Workstream, None) >def __init__(self, *args, **kwargs): >super(AssetForm, self).__init__(*args, **kwargs) >if self.instance: >self.fields['workstreams'].queryset = > Workstream.objects.filter(project__id=args[0]) >self.fields['workstreams'].widget.choices = > self.base_fields['workstreams'].choices > > I instantiate it > > f = AssetForm(1) # 1 is the number of a project, which I filter with. > > It seems to work, and I can poke around at the fields and choices. > > but when I try to render it: > > f.as_p() > > I get: > > Traceback (most recent call last): > File "", line 1, in > File "django\django\forms\forms.py", line 191, in as_p >return self._html_output(u'%(label)s %(field)s%(help_text)s p>', u'%s', > '', u' %s', True) > File "django\django\forms\forms.py", line 139, in _html_output >top_errors = self.non_field_errors() # Errors that should be > displayed above > all fields. > File "django\django\forms\forms.py", line 199, in non_field_errors >return self.errors.get(NON_FIELD_ERRORS, self.error_class()) > File "django\djang > o\forms\forms.py", line 111, in _get_errors >self.full_clean() > File "django\django\forms\forms.py", line 218, in full_clean >value = field.widget.value_from_datadict(self.data, self.files, > self.add_pre > fix(name)) > File "django\django\forms\widgets.py", line 170, in > value_from_datadict >return data.get(name, None) > AttributeError: 'int' object has no attribute 'get' > > > Any ideas? > > Thanks, > Justin > > > > You need to remove the 1 from args before instantiating the Form, so something like: def __init__(self, *args, **kwargs): id = args[0] args = args[1:] etc... Otherwise django thinks 1 is the first argument which is supposed to be the data dictionary.. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Error when Rendering form with ModelChoiceField
Thanks Alex. That works great. Just wondering: It seems like this causes the form to be bound, which causes it to render all its errors. I can set .is_bound to False manually to get around this, is this what I should be doing? On Jun 3, 5:15 pm, Alex Gaynor wrote: > On Wed, Jun 3, 2009 at 5:12 PM, justind wrote: > > > Hello, > > > I'm using the following to provide a select box with filtered choices: > > > class AssetForm(ModelForm): > > """Asset form takes a project id. It only allows workstreams > > attached to > > this project name.""" > > workstreams = forms.ModelChoiceField(Workstream, None) > > def __init__(self, *args, **kwargs): > > super(AssetForm, self).__init__(*args, **kwargs) > > if self.instance: > > self.fields['workstreams'].queryset = > > Workstream.objects.filter(project__id=args[0]) > > self.fields['workstreams'].widget.choices = > > self.base_fields['workstreams'].choices > > > I instantiate it > > > f = AssetForm(1) # 1 is the number of a project, which I filter with. > > > It seems to work, and I can poke around at the fields and choices. > > > but when I try to render it: > > > f.as_p() > > > I get: > > > Traceback (most recent call last): > > File "", line 1, in > > File "django\django\forms\forms.py", line 191, in as_p > > return self._html_output(u'%(label)s %(field)s%(help_text)s > p>', u'%s', > > '', u' %s', True) > > File "django\django\forms\forms.py", line 139, in _html_output > > top_errors = self.non_field_errors() # Errors that should be > > displayed above > > all fields. > > File "django\django\forms\forms.py", line 199, in non_field_errors > > return self.errors.get(NON_FIELD_ERRORS, self.error_class()) > > File "django\djang > > o\forms\forms.py", line 111, in _get_errors > > self.full_clean() > > File "django\django\forms\forms.py", line 218, in full_clean > > value = field.widget.value_from_datadict(self.data, self.files, > > self.add_pre > > fix(name)) > > File "django\django\forms\widgets.py", line 170, in > > value_from_datadict > > return data.get(name, None) > > AttributeError: 'int' object has no attribute 'get' > > > Any ideas? > > > Thanks, > > Justin > > You need to remove the 1 from args before instantiating the Form, so > something like: > > def __init__(self, *args, **kwargs): > id = args[0] > args = args[1:] > etc... > > Otherwise django thinks 1 is the first argument which is supposed to be the > data dictionary.. > > Alex > -- > "I disapprove of what you say, but I will defend to the death your right to > say it." --Voltaire > "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Error when Rendering form with ModelChoiceField
On Thu, Jun 4, 2009 at 9:38 AM, justind wrote: > > Thanks Alex. That works great. > > Just wondering: It seems like this causes the form to be bound, which > causes it to render all its errors. > I can set .is_bound to False manually to get around this, is this what > I should be doing? > > On Jun 3, 5:15 pm, Alex Gaynor wrote: > > On Wed, Jun 3, 2009 at 5:12 PM, justind wrote: > > > > > Hello, > > > > > I'm using the following to provide a select box with filtered choices: > > > > > class AssetForm(ModelForm): > > >"""Asset form takes a project id. It only allows workstreams > > > attached to > > >this project name.""" > > >workstreams = forms.ModelChoiceField(Workstream, None) > > >def __init__(self, *args, **kwargs): > > >super(AssetForm, self).__init__(*args, **kwargs) > > >if self.instance: > > >self.fields['workstreams'].queryset = > > > Workstream.objects.filter(project__id=args[0]) > > >self.fields['workstreams'].widget.choices = > > > self.base_fields['workstreams'].choices > > > > > I instantiate it > > > > > f = AssetForm(1) # 1 is the number of a project, which I filter with. > > > > > It seems to work, and I can poke around at the fields and choices. > > > > > but when I try to render it: > > > > > f.as_p() > > > > > I get: > > > > > Traceback (most recent call last): > > > File "", line 1, in > > > File "django\django\forms\forms.py", line 191, in as_p > > >return self._html_output(u'%(label)s %(field)s%(help_text)s > > p>', u'%s', > > > '', u' %s', True) > > > File "django\django\forms\forms.py", line 139, in _html_output > > >top_errors = self.non_field_errors() # Errors that should be > > > displayed above > > > all fields. > > > File "django\django\forms\forms.py", line 199, in non_field_errors > > >return self.errors.get(NON_FIELD_ERRORS, self.error_class()) > > > File "django\djang > > > o\forms\forms.py", line 111, in _get_errors > > >self.full_clean() > > > File "django\django\forms\forms.py", line 218, in full_clean > > >value = field.widget.value_from_datadict(self.data, self.files, > > > self.add_pre > > > fix(name)) > > > File "django\django\forms\widgets.py", line 170, in > > > value_from_datadict > > >return data.get(name, None) > > > AttributeError: 'int' object has no attribute 'get' > > > > > Any ideas? > > > > > Thanks, > > > Justin > > > > You need to remove the 1 from args before instantiating the Form, so > > something like: > > > > def __init__(self, *args, **kwargs): > >id = args[0] > >args = args[1:] > >etc... > > > > Otherwise django thinks 1 is the first argument which is supposed to be > the > > data dictionary.. > > > > Alex > > -- > > "I disapprove of what you say, but I will defend to the death your right > to > > say it." --Voltaire > > "The people's good is the highest law."--Cicero > > > No, you should be doing more or less exactly what I specified in my earlier post to ensure tha tyou don't actaully pass the integer to the parent constructor. Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---