Given a model, how can I get a widget for each field?

32 views
Skip to first unread message

Bernd Wechner

unread,
Mar 20, 2018, 7:37:00 AM3/20/18
to Django users
Pulling my hair out again.

I don't have a form, nor want a form, stunning really, but I just have the model. And I want the field widgets.

I can't seem to find them anywhere. I know you can specify widget= on form fields when declaring the model, but can't find it anywhere in the data structure.

Before you go asking why (really prefer some though invested into answer questions over questioning questions ;-), the need is simple enough. I have a list view, and at the top I want to put some filter options. So for selected fields in the model being listed, I want the widget to display at top along with operators. =, < > etc ... so I can filter the list view with some controls. But the filter needs the appropriate widget given a model field. Sort of model._meta.fields[n].widget, but not, as that doesn't work.

Of course I'm in a ListView when I need it, and so have access to it as well, but again can find no form or widgets there.

Spent so long searching doco and the web already I'm either blind (could be) or this is just bothersomely tricky!

Regards,

Bernd.

Cictani

unread,
Mar 20, 2018, 8:12:06 AM3/20/18
to Django users

Without a form you can't get the Widget which is used by default. Are the fields always the same? If yes you could create a ModelForm on the fly and render the fields manually.

Best regards,

Andreas

Andréas Kühne

unread,
Mar 20, 2018, 8:42:16 AM3/20/18
to django...@googlegroups.com
Hi,

Like Andreas is saying the only way to define the default widgets is to use a form - and the simplest way to define a form is to use a ModelForm. 

When you use Django you should do best by trying to work WITH the framework instead of working against it. Forms are used for connecting templates with model instances - that's the way django wants it to be. You would have spent less time by using a ModelForm (which is like 5 lines of code) - than searching for some other way of doing it. Really try to look at the way django handles information and you will see that forms make sense in your usecase.

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ad20b07a-bf75-4032-9d52-ace38e7b8125%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bernd Wechner

unread,
Mar 20, 2018, 10:08:31 PM3/20/18
to Django users
Andréas and Andreas,

I admit I smiled to see Andréas seconding Andreas' post ;-). One my best friends in primary school was an Andreas ;-).

Given I have no form and don't want one, ModelForm in its declarative for seems undesirable. But your thinking is good and I realise in response and in retrospect that I could create a ModelForm on the fly with with modelform_factory, and extract widgets from that.

Thanks for the pointer. I wasn't thinking in the direction of a form alas because I haven't got one in this context, and so looking at ways to get a widget form a model, and it seems modelform_factory is the path forward.

Kind regards,

Bernd.


On Tuesday, 20 March 2018 23:42:16 UTC+11, Andréas Kühne wrote:
Hi,

Like Andreas is saying the only way to define the default widgets is to use a form - and the simplest way to define a form is to use a ModelForm. 

When you use Django you should do best by trying to work WITH the framework instead of working against it. Forms are used for connecting templates with model instances - that's the way django wants it to be. You would have spent less time by using a ModelForm (which is like 5 lines of code) - than searching for some other way of doing it. Really try to look at the way django handles information and you will see that forms make sense in your usecase.

Regards,

Andréas

2018-03-20 13:12 GMT+01:00 Cictani <andreas...@gmail.com>:

Without a form you can't get the Widget which is used by default. Are the fields always the same? If yes you could create a ModelForm on the fly and render the fields manually.

Best regards,

Andreas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Bernd Wechner

unread,
Mar 21, 2018, 5:48:57 AM3/21/18
to Django users
Actually I stand corrected, no need to build a format all. django.forms.fields_for_model provides just the fields for model and their widgets. Awesome. Neat and tidy.
Reply all
Reply to author
Forward
0 new messages