Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango Dynamic Widget/Form field representation
    primarykey
    data
    text
    <p>I have a table that contains various types of settings. I want to display and validate each setting differently in a form. For example if the setting is for an integer or for a string.</p> <p>This is my model</p> <pre><code>class Setting(models.Model): SETTING_TYPES = ( (u'1', u'Decimal'), (u'2', u'Integer'), (u'3', u'String'), ) type = models..CharField(max_length=2, choices=SETTING_TYPES) name = models.CharField(max_length=50, unique=True) value = models.CharField(max_length=200) </code></pre> <p>So if the type is 1 I want to restrict form entry and validate against the fact that I'm expecting a decimal. But if the type is 3, I need to show a bigger entry form and validate against the fact im expecting a string.</p> <p>A few places I can see with possibility to do this is:</p> <ol> <li>In a custom widget</li> <li>By overriding the BaseModelFormSet class to change field properties on init (<a href="http://snippets.dzone.com/posts/show/7936" rel="nofollow noreferrer">http://snippets.dzone.com/posts/show/7936</a>)</li> <li>by using a custom template filter that will evaluate what the type is and then render the form manually via a custom template for that setting type</li> <li>store the type and value together in a jsonfield and use something like <a href="http://www.huyng.com/archives/django-custom-form-widget-for-dictionary-and-tuple-key-value-pairs/661/" rel="nofollow noreferrer">http://www.huyng.com/archives/django-custom-form-widget-for-dictionary-and-tuple-key-value-pairs/661/</a> to alter the display and validation of the setting value in one place..</li> </ol> <p>With option 1, i'm not sure if I use a custom widget on the 'value' field if it will be able to access the 'type' field to determine how to represent the 'value'</p> <p>I tried option 2, and while I could access the form elements, I could not access the form values to determine the type of the setting (maybe they weren't bound yet?). I think this route might be the simpliest but I will need access to form values..</p> <pre><code>class BaseSettingFormset(BaseModelFormSet): def __init__(self, *args, **kwargs): super(BaseSettingFormset, self).__init__(*args, **kwargs) for form in self.forms: # here i would access the type value and then change the widget propeties below if form.fields['type'] == 3: # DOESN"T WORK :( # change the widget and field properties form.fields['value'].help_text='some text' form.fields['value'].widget = CustomWidget() # or switch from decimal to string widgets.. def editProfile(request, strategy_id): settingModelFormset = modelformset_factory(profile, formset=BaseSettingFormset) ... retrieve the values and show them.. </code></pre> <p>(while posting this i did find a post that may allow me to bind the form during the init and therefore have access to the value, see <a href="https://stackoverflow.com/questions/1226590/django-how-can-i-access-the-form-field-from-inside-a-custom-widget/2135739#2135739">django - how can I access the form field from inside a custom widget</a> )</p> <p>Option 3 works for displaying the field, but not for validating any data. I think I would need to combine it with option 2 to validate it during save.</p> <pre><code>#Custom template filter @register.filter(name='show_setting') def show_setting(form): setting = field_value(form['type']) # calls another function to retrieve value (works ok) # render page using custom template that can alter the form to show a small field or text area etc. # can also add js validations here.. (but no server validations) if setting: return render_to_string('setting-'+str(setting)+'.html', { 'form': form }) </code></pre> <p>.</p> <p>Which method would be best (or is there another way), and how do I complete it? Or am I going about this the wrong way, is there a more djangoesc way to solve my problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload