#27370: Django's Select widget adds a required="required" attribute, even if
created with empty_label=True
-------------------------------------+-------------------------------------
     Reporter:  alexpirine           |                    Owner:  Josef
         Type:                       |  Rousek
  Cleanup/optimization               |                   Status:  closed
    Component:  Forms                |                  Version:  master
     Severity:  Normal               |               Resolution:  fixed
     Keywords:                       |             Triage Stage:  Ready for
  HTML,Select,wdiget,ModelChoiceField|  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Aurélien Pardon):

 There is a huge performance regression as a side-effect of
 [changeset:"aaecf038cae61f114db396f74e06759c95f21e93" aaecf038]. For
 {{{ModelChoiceField}}} with {{{empty_label=None}}}, it doubles the queries
 to the database.

 With this {{{simple_app/models.py}}}:
 {{{#!python
 from django.db import models

 class MyModel(models.Model):
     my_field = models.CharField(max_length=1)
 }}}

 the following code:
 {{{#!python
 from django import forms
 from simple_app.models import MyModel

 class MyForm(forms.Form):
     my_form_field = forms.ModelChoiceField(MyModel.objects.all(),
 empty_label=None)

 MyForm().as_ul()
 }}}

 makes two times the same request to the database:
 {{{#!python
 >>> from django.db import connection
 >>> from django.db import reset_queries
 >>> reset_queries()
 >>> _ = MyForm().as_ul()
 >>> connection.queries
 [{'sql': 'SELECT "simple_app_mymodel"."id",
 "simple_app_mymodel"."my_field" FROM "simple_app_mymodel"',
   'time': '0.000'},
  {'sql': 'SELECT "simple_app_mymodel"."id",
 "simple_app_mymodel"."my_field" FROM "simple_app_mymodel"',
   'time': '0.000'}]
 }}}

 Before this commit, it only makes one request (as intended IMO):
 {{{#!python
 >>> from django.db import connection
 >>> from django.db import reset_queries
 >>> reset_queries()
 >>> _ = MyForm().as_ul()
 >>> connection.queries
 [{'sql': 'SELECT "simple_app_mymodel"."id",
 "simple_app_mymodel"."my_field" FROM "simple_app_mymodel"',
   'time': '0.000'}]
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/27370#comment:15>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.4e08f4ab70684d6fd783d10b66115ace%40djangoproject.com.

Reply via email to