On Tue, Jul 8, 2014 at 3:16 PM, Ram Ganesh <
ramga...@gmail.com> wrote:
>
>
> it shows a drop down menu which is have mobile objects.
> How to get mobile name?
https://docs.djangoproject.com/en/1.7/ref/forms/fields/#modelchoicefield
"""
The __str__ (__unicode__ on Python 2) method of the model will be
called to generate string representations of the objects for use in
the field’s choices; to provide customized representations, subclass
ModelChoiceField and override label_from_instance.
"""
So you can define a __unicode__ or __str__ method (python 2, python 3)
on your Mobile model, or define a sub-class of ModelChoiceField that
has a label_from_instance() method and use that instead of
ModelChoiceField.
> confused about - self.queryset =
> forms.ModelChoiceField(queryset=Mobile.objects.all(label="select mob")
Well that line is quite confusing - I guess you are trying to set the
queryset that the ModelChoiceField will use, but that is not how you
do it:
Foo.objects.all() does not take arguments.
Model forms do not use a self.queryset attribute.
For a ModelChoiceField, Foo.objects.all() is the default queryset.
I guess you want a form like this:
class AgentForm(ModelForm):
mobile = forms.ModelChoiceField(label='Select mobile')
class Meta:
model = Agent
def __init__(self, *args, **kwargs):
super(AgentForm, self).__init__(*args, **kwargs)
self.fields['mobile'].queryset = Mobile.objects.all()
As I mentioned, Foo.objects.all() is the default, so if you are happy
with that, omit the entire __init__ method.
Cheers
Tom