Tuesday, March 20, 2018

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

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.
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.

--
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-users@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/14643d7a-5a58-4e72-80f8-62b123afbce8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment