Ticket #7287: newforms-initial-model.diff

File newforms-initial-model.diff, 821 bytes (added by Simon Litchfield <simon@…>, 7 years ago)

Cleans initial values that are model instances into their pk values

  • django/newforms/forms.py

     
    278278        attributes passed as attrs.  If no widget is specified, then the
    279279        field's default widget will be used.
    280280        """
     281        from django.db import models
    281282        if not widget:
    282283            widget = self.field.widget
    283284        attrs = attrs or {}
     
    288289            data = self.form.initial.get(self.name, self.field.initial)
    289290            if callable(data):
    290291                data = data()
     292            elif isinstance(data, models.Model):
     293                data = data.pk
    291294        else:
    292295            data = self.data
    293296        return widget.render(self.html_name, data, attrs=attrs)
Back to Top