djangosnippets.org: Latest snippets tagged with 'modelmultiplechoicefield'http://djangosnippets.org/tags/modelmultiplechoicefield/2008-12-31T06:38:43.871329-06:00Lazy options on ModelForm fields - like setting a ModelChoiceField queryset from the view
2008-12-31T06:38:43.871329-06:00jpichttp://djangosnippets.org/snippets/1272/<p>Example view code:</p>
<pre><code>lazy_field_options = {
'field_name_that_is_m2m': {
'queryset': YourRelatedModel.objects.filter(groups=request.user.groups.all()),
},
'field_name_that_is_fk': {
'queryset': YourOtherRelatedModel.objects.filter(slug=request_slug),
},
}
modelform = YourModelForm(jpic_field_options=lazy_field_options)
# after the modelform has called for parent __init__, it will set
# options for each field if possible.
</code></pre>
Freely redistributable