0

Off of this question, I have an Action model that has a foreign key which specifies how often an action reoccurs:

class Reoccurance(models.Model):
    label = models.CharField("Label", max_length=50, unique = True)
    days = models.IntegerField("Days")

    def __unicode__(self):
        return self.label

    class Meta:
        ordering = ['days']

class Action(models.Model):
    name = models.CharField("Action Name", max_length=200, unique = True)
    complete = models.BooleanField(default=False, verbose_name="Complete?")
    reoccurance = models.ForeignKey(Reoccurance, blank=True, null=True, verbose_name="Reoccurance")

I'm making a modelForm of Action that results in HTML code for reoccurance (based on the database values that exist for the Reoccurance table):

<select id="id_reoccurance" class="selectwithtitles" name="reoccurance">
    <option value="" title="" >---------</option>
    <option value="12" title="2" >2 Days</option>
    <option value="1" title="3" >3 Days</option>
    <option value="2" title="5" >5 Days</option>
    <option value="10" title="6" >6 Days</option>
    <option value="9" title="7" >1 Week</option>
    <option value="3" title="10" >10 Days</option>
    <option value="4" title="14" >2 Weeks</option>
    <option value="11" title="21" >3 Weeks</option>
    <option value="5" title="30" >1 Month</option>
    <option value="13" title="42" >6 Weeks</option>
    <option value="6" title="90" >1 Quarter</option>
    <option value="7" title="180" >6 Months</option>
    <option value="8" title="365" >1 Year</option>
</select>

I generate the titles by a subclassed field:

from django.utils.html import conditional_escape, escape
from django.utils.encoding import force_unicode

class SelectWithTitles(forms.Select):
    def __init__(self, *args, **kwargs):
        super(SelectWithTitles, self).__init__(*args, **kwargs)
        # Ensure the titles dict exists
        self.titles = {}

    def render_option(self, selected_choices, option_value, option_label):
        title_html = (option_label in self.titles) and \
            u' title="%s" ' % escape(force_unicode(self.titles[option_label])) or ''
        option_value = force_unicode(option_value)
        selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
        return u'<option value="%s"%s%s>%s</option>' % (
            escape(option_value), title_html, selected_html,
            conditional_escape(force_unicode(option_label)))

class ChoiceFieldWithTitles(forms.ChoiceField):
    widget = SelectWithTitles

    def __init__(self, choices=(), *args, **kwargs):
        choice_pairs = [(c[0], c[1]) for c in choices]
        super(ChoiceFieldWithTitles, self).__init__(choices=choice_pairs, *args, **kwargs)
        self.widget.titles = dict([(c[1], c[2]) for c in choices])

    def clean(self, value):
        self.required = False
        if not value and not self.required:
            return value
        return super(ChoiceFieldWithTitles, self).clean(value)

class ActionForm(forms.ModelForm):
    reoccurance = ChoiceFieldWithTitles()

    def __init__(self, *args, **kwargs):
        super(ActionForm, self).__init__(*args, **kwargs)   

        choices = []
        for pt in Reoccurance.objects.all():
            choices.append((pt.id, pt.label, pt.days))
        self.fields['reoccurance'] = ChoiceFieldWithTitles(choices = choices)

    def clean(self):
        cleaned_data = super(ActionForm, self).clean()
        if cleaned_data.get("reoccurance") != '':
            rec = Reoccurance.objects.get(pk=cleaned_data.get("reoccurance"))
            self.cleaned_data['reoccurance'] = rec
        return super(ActionForm, self).clean()

I had to add the clean method to ActionForm because I had to return the database Reoccurance object, not simply it's id.

I also had to add a clean method to the ChoiceFieldWithTitles because I could not get it to allow a blank value for reoccurance. It also saw it as required, even when I used reoccurance = ChoiceFieldWithTitles(required=False).

My issue now is that I can't seem to allow a blank value. If the user selects the first option of the drop down:

<option value="" title="" >---------</option>

The form returns this error:

ValueError at /next_actions/test-action-9712,778/edit/
Cannot assign "u''": "Action.reoccurance" must be a "Reoccurance" instance.

What do I need to do to the ActionForm's clean method to allow a blank value for the reoccurance field?

Community
  • 1
  • 1
Ed.
  • 4,439
  • 10
  • 60
  • 78

1 Answers1

0

No, all you need to do is use the proper base field type. If you're dealing with a foreign key or an M2M relationship, the field should be a ModelChoiceField type, not ChoiceField.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • You're suggesting that I need to say ChoiceFieldWithTitles(forms.ModelChoiceField)? When I try this, I get a TypeError: __init__() takes at least 2 arguments (2 given). This occurs at the line super(ChoiceFieldWithTitles, self).__init__(choices=choice_pairs, *args, **kwargs) – Ed. Sep 07 '12 at 19:39
  • That's because that bit is unnecessary. `ModelChoiceField` doesn't take `choices` it takes a `queryset`, and it *automatically* creates the appropriate choices from the queryset. That's point. All you need to worry about is your custom rendering in your subclass. – Chris Pratt Sep 07 '12 at 19:46
  • ok. I believe you're saying that I don't need the ChoiceFieldWithTitles. Then how do I hook the ModelChoiceField to the custom rendering (SelectWithTitles) so that I can pull an additional data field into the template? – Ed. Sep 07 '12 at 19:54
  • I think I got it. Thanks: http://stackoverflow.com/questions/6477856/how-to-add-attributes-to-option-tags-in-django – Ed. Sep 07 '12 at 20:30