#11390: If you use a callable as default value on a model field, it gets called 
3
times.
----------------------------------+------------------------------------
     Reporter:  espen.nettbruk@…  |                    Owner:  (none)
         Type:  Bug               |                   Status:  new
    Component:  Forms             |                  Version:  1.0
     Severity:  Normal            |               Resolution:
     Keywords:  models            |             Triage Stage:  Accepted
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+------------------------------------

Comment (by MaartenPI):

 I created stack traces and don't really see a clean way to optimize this
 any further. I'll close the ticket, if anyone feels it shouldn't. Please
 re-open.

 As reference here are the stack traces:

 from django.forms.models import modelform_factory
 from cad.models import Article
 article_modelform = modelform_factory(Article, fields=('title',))
 form = article_modelform()
 ==> gives the following stacktrace:

 ~/local/lib/python2.7/site-packages/django/forms/models.py(278)__init__()
 -> self.instance = opts.model()
 ~/local/lib/python2.7/site-
 packages/django/db/models/base.py(529)__init__()
 -> val = field.get_default()
 ~/local/lib/python2.7/site-
 packages/django/db/models/fields/__init__.py(769)get_default()
 -> return self.default()
 ~/callable_as_default/sample_project/cad/models.py(9)get_title()

 form.as_p()
 ==> gives the following stacktrace
 ~/local/lib/python2.7/site-packages/django/forms/forms.py(289)as_p()
 -> errors_on_separate_row=True)
 ~/local/lib/python2.7/site-
 packages/django/forms/forms.py(226)_html_output()
 -> 'field': six.text_type(bf),
 ~/local/lib/python2.7/site-packages/django/utils/html.py(382)<lambda>()
 -> klass.__unicode__ = lambda self: mark_safe(klass_unicode(self))
 ~/local/lib/python2.7/site-
 packages/django/forms/boundfield.py(42)__str__()
 -> return self.as_widget() + self.as_hidden(only_initial=True)
 ~/local/lib/python2.7/site-
 packages/django/forms/boundfield.py(89)as_widget()
 -> attrs = self.build_widget_attrs(attrs, widget)
 ~/local/lib/python2.7/site-
 packages/django/forms/boundfield.py(238)build_widget_attrs()
 -> if widget.use_required_attribute(self.initial) and self.field.required
 and self.form.use_required_attribute:
 ~/local/lib/python2.7/site-
 packages/django/forms/boundfield.py(225)initial()
 -> data = data()
 ~/callable_as_default/sample_project/cad/models.py(9)get_title()

--
Ticket URL: <https://code.djangoproject.com/ticket/11390#comment:17>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/082.d0d9d5597bd050259e235ac6cf87b6bf%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to