简体   繁体   中英

How I can use correctly django ModelChoiceField

I'm a newbie and right now I'm using Django forms to show some fields. One of these fields is a ModelChoiceField this show correctly model data, but I don´t know how I can fill a CharField later of select a option in the ModelChoiceField.

How I can make send the value of option selected for obtain the data that i need for later show this in a CharField, this is possible make directly from a djangoForm or do I need a new view to return the value?

There are two ways to achieve what you want I think:

  1. Provide an initial value for your form value.
  2. Bind your form to some data.

See this related stack overflow to provide initial values .

See Django's documentation on forms for binding data. There are two kinds of form states: bound and unbound . To create a bound form, you just need to create an instance of your form with some data: MyForm({'data-key1': 'data-value1', ...}) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM