Login

Tag "ModelChoiceField"

Snippet List

Grouped Model Choice Field

This class lets me have a model choice field that includes optgroups . Django has built-in support for optgroups if you explicitly set all the choices in a ChoiceField, but that doesn't help with ModelChoiceFields. Optgroups let you have nice-looking subscategories in huge dropdowns. They're even more useful if you're using something like selectize.js, because you have a ton of options. If you inherit from GroupedModelChoiceField and override the optgroup_from_instance function, as in SampleChoiceField, you'll get a dropdown with your models with the expected optgroup tags in the html. Be sure to have your queryset first order by whatever you're displaying in optgroup.

  • ModelChoiceField
  • OptGroup
  • Forms
Read More

ModelChoiceField with choice groups for recursive relationships

Note: must call within __init__() method, so you must do self.fields["field"] = ModelChoiseField(...). This is because I did not use a ModelChoiceIterator. A subclass of ModelChoiceField which represents the tree level of each node when generating option labels. It's limited to one level of nesting, if you need more, you should consider the django-mptt package. For example, where a form which used a ModelChoiceField: category = ModelChoiceField(queryset=Category.objects.all()) ...would result in a select with the following options: --------- Root 1 Root 2 Child 1.1 Child 1.2 Child 2.1 Using a NestedModelChoiceField instead: category = NestedModelChoiceField(queryset=Category.objects.all(), related_name='category_set', parent_field='parent_id', label_field='title') ...would result in a select with the following options: Root 1 --- Child 1.1 --- Child 1.2 Root 2 --- Child 2.1

  • ModelChoiceField
Read More

A Lazy ModelChoiceField implementation

Sometimes we may need to generate a *ModelChoiceField* in which choices are generated at runtime, depending on the locale language. The snippet generates a *ChoiceField* based on a queryset and a specific attribute of the Model, ordering the choices by the attribute content in the locale language. **Usage example** (inside a form declaration) country = LazyModelChoiceField(sort_by='name', queryset = \ Country.objects.all, empty_label=_('All countries'), label=_('Country')) Based on lsbardel LazyChoiceField implementation (snippet 1767)

  • forms
  • model
  • field
  • ModelChoiceField
Read More

A ModelChoiceField with support for title in options based on a field in the model

ModelChoiceTitleField is a ModelChoiceField descendent that creates <OPTIONS> with title elements based on the field specified in title_source_field: priority=ModelChoiceTitleField(Priority.objects.all(), initial=Priority.objects.get(default=True).id, title_source_field='long_description') That will generate a `<SELECT>` element looking like: <select name="priority" id="id_priority"> <option value="1" title="Some extremely important task.">Emergency</option> ... </select> In the `<option>` above, the title was retrieved from the `long_description` field for the instance of the Priority class. The word Emergency came from a call to the instance of the Priority class' `__unicode__()` method. The value of the option is the PK for the instance of the Priority class.

  • ModelChoiceField
Read More

5 snippets posted so far.