#33729: AutocompleteSelect widget broken after moving from django 2.2 to django 
3.2
------------------------------------+--------------------------------------
     Reporter:  exo                 |                    Owner:  nobody
         Type:  Bug                 |                   Status:  closed
    Component:  contrib.admin       |                  Version:  3.2
     Severity:  Normal              |               Resolution:  invalid
     Keywords:  AutocompleteSelect  |             Triage Stage:  Unreviewed
    Has patch:  0                   |      Needs documentation:  0
  Needs tests:  0                   |  Patch needs improvement:  0
Easy pickings:  0                   |                    UI/UX:  0
------------------------------------+--------------------------------------
Description changed by exo:

Old description:

> Hello :) After upgrading `Django` version to `3.2`, widget
> `AutocompleteSelect` that I use in `django` admin panel (to have a drop-
> down from which I can choose an object) **is broken**.
>
> The error I see is
> {{{
>     AttributeError at /admin/question/
>     'QuerySet' object has no attribute 'name'
>
>     Request Method:     GET
>     Request URL:        http://localhost:8000/admin/question/
>     Django Version:     3.2.13
>     Exception Type:     AttributeError
>     Exception Value:
>     'QuerySet' object has no attribute 'name'
>     Exception Location: /home/django-app/env/lib/python3.8/site-
> packages/django/contrib/admin/widgets.py, line 412, in build_attrs
>     Python Executable:  /home/django-app/env/bin/python3
>     Python Version:     3.8.10
>     Python Path:
>     ['/home/django-app/testsite',
>      '/usr/lib/python38.zip',
>      '/usr/lib/python3.8',
>      '/usr/lib/python3.8/lib-dynload',
>      '/home/django-app/env/lib/python3.8/site-packages']
>     Server time:        Fri, 20 May 2022 10:13:27 +0000
>     Error during template rendering
>     In template /home/django-
> app/testsite/polls/templates/admin/question_export.html, error at line 18
>
>     'QuerySet' object has no attribute 'name'
>     11
>     12  {% block content %}
>     13      <div id="content-main">
>     14        <p>Select question to export:</p>
>     15          <form method="post" enctype="multipart/form-data">
>     16              {% csrf_token %}
>     17              <table>
>     18                  {{form.as_table}}
>     19              </table>
>     20              <div class="submit-row">
>     21                  <input type="submit" value="Export Question" />
>     22              </div>
>     23          </form>
>     24      </div>
>     25      {{form.media}}
>     26  {% endblock %}
>     27
> }}}
> `AutocompleteSelect` inherits from `AutocompleteMixin`
>
> When I compare `AutocompleteMixin` for `django 3.2` and `django 2.2`
> https://github.com/django/django/blob/3.2.13/django/contrib/admin/widgets.py#L410-L412
> https://github.com/django/django/blob/2.2.7/django/contrib/admin/widgets.py#L411
>
> I see that they added new attributes
> {{{
>     'data-app-label': self.field.model._meta.app_label,
>     'data-model-name': self.field.model._meta.model_name,
>     'data-field-name': self.field.name,
> }}}
> in `django 3.2`
> but there is no `name` on `self.field` and probably that's why I get this
> error.
>

> The code looks like this
>
> view.py
> {{{
>     from django.forms import ModelChoiceField
>     from django import forms
>     from django.contrib import admin
>     from django.contrib.admin.widgets import AutocompleteSelect
>
>     class QuestionChoiceField(ModelChoiceField):
>         widget = AutocompleteSelect(Question.objects.all(), admin.site)
>

>     class QuestionExportForm(forms.Form):
>         question = QuestionChoiceField(queryset=Question.objects.all(),
> required=True)
>
>         def clean_question(self):
>             return self.cleaned_data["question"]
>

>     class QuestionExportView(FormView):
>         template_name = "admin/question_export.html"
>         form_class = QuestionExportForm
>         success_url = "/admin/"
>
>         def form_valid(self, form):
>             question = form.cleaned_data.get("question")
>             return generate_response(question)
> }}}
>
> models.py
> {{{
>     from django.db import models
>

>     class Question(models.Model):
>         uuid = models.UUIDField(primary_key=True, default=uuid.uuid4,
> editable=False)
>         question_text = models.CharField(max_length=200)
>         pub_date = models.DateTimeField('date published')
>
> }}}
> templates/admin/question_export.html
> {{{
>     {% block content %}
>         <div id="content-main">
>           <p>Select question to export:</p>
>             <form method="post" enctype="multipart/form-data">
>                 {% csrf_token %}
>                 <table>
>                     {{form.as_table}}
>                 </table>
>                 <div class="submit-row">
>                     <input type="submit" value="Export Question" />
>                 </div>
>             </form>
>         </div>
>         {{form.media}}
>     {% endblock %}
> }}}
> urls.py
> {{{
> from django.contrib.admin.views.decorators import staff_member_required
> from django.urls import path
>
> from polls.views import QuestionExportView
>
> app_name = "question_exports"
>

> urlpatterns = [
>     path("admin/question/",
> staff_member_required(QuestionExportView.as_view()), name="question"),
>     path('admin/', admin.site.urls),
> ]
>
> }}}
>
> How can I approach this issue? Any help would be appreciated :)!

New description:

 Hello :) After upgrading `Django` version to `3.2`, widget
 `AutocompleteSelect` that I use in `django` admin panel (to have a drop-
 down from which I can choose an object) **is broken**.

 The error I see is
 {{{
     AttributeError at /admin/question/
     'QuerySet' object has no attribute 'name'

     Request Method:     GET
     Request URL:        http://localhost:8000/admin/question/
     Django Version:     3.2.13
     Exception Type:     AttributeError
     Exception Value:
     'QuerySet' object has no attribute 'name'
     Exception Location: /home/django-app/env/lib/python3.8/site-
 packages/django/contrib/admin/widgets.py, line 412, in build_attrs
     Python Executable:  /home/django-app/env/bin/python3
     Python Version:     3.8.10
     Python Path:
     ['/home/django-app/testsite',
      '/usr/lib/python38.zip',
      '/usr/lib/python3.8',
      '/usr/lib/python3.8/lib-dynload',
      '/home/django-app/env/lib/python3.8/site-packages']
     Server time:        Fri, 20 May 2022 10:13:27 +0000
     Error during template rendering
     In template /home/django-
 app/testsite/polls/templates/admin/question_export.html, error at line 18

     'QuerySet' object has no attribute 'name'
     11
     12  {% block content %}
     13      <div id="content-main">
     14        <p>Select question to export:</p>
     15          <form method="post" enctype="multipart/form-data">
     16              {% csrf_token %}
     17              <table>
     18                  {{form.as_table}}
     19              </table>
     20              <div class="submit-row">
     21                  <input type="submit" value="Export Question" />
     22              </div>
     23          </form>
     24      </div>
     25      {{form.media}}
     26  {% endblock %}
     27
 }}}
 `AutocompleteSelect` inherits from `AutocompleteMixin`

 When I compare `AutocompleteMixin` for `django 3.2` and `django 2.2`
 
https://github.com/django/django/blob/3.2.13/django/contrib/admin/widgets.py#L410-L412
 
https://github.com/django/django/blob/2.2.7/django/contrib/admin/widgets.py#L411

 I see that they added new attributes
 {{{
     'data-app-label': self.field.model._meta.app_label,
     'data-model-name': self.field.model._meta.model_name,
     'data-field-name': self.field.name,
 }}}
 in `django 3.2`
 but there is no `name` on `self.field` and probably that's why I get this
 error.


 The code looks like this

 view.py
 {{{
     from django.forms import ModelChoiceField
     from django import forms
     from django.contrib import admin
     from django.contrib.admin.widgets import AutocompleteSelect
     from django.http import HttpResponse

     def generate_response(question):
         # some code
         return HttpResponse(content_type="text/csv")

     class QuestionChoiceField(ModelChoiceField):
         widget = AutocompleteSelect(Question.objects.all(), admin.site)


     class QuestionExportForm(forms.Form):
         question = QuestionChoiceField(queryset=Question.objects.all(),
 required=True)

         def clean_question(self):
             return self.cleaned_data["question"]


     class QuestionExportView(FormView):
         template_name = "admin/question_export.html"
         form_class = QuestionExportForm
         success_url = "/admin/"

         def form_valid(self, form):
             question = form.cleaned_data.get("question")
             return generate_response(question)
 }}}

 models.py
 {{{
     from django.db import models


     class Question(models.Model):
         uuid = models.UUIDField(primary_key=True, default=uuid.uuid4,
 editable=False)
         question_text = models.CharField(max_length=200)
         pub_date = models.DateTimeField('date published')

 }}}
 templates/admin/question_export.html
 {{{
     {% block content %}
         <div id="content-main">
           <p>Select question to export:</p>
             <form method="post" enctype="multipart/form-data">
                 {% csrf_token %}
                 <table>
                     {{form.as_table}}
                 </table>
                 <div class="submit-row">
                     <input type="submit" value="Export Question" />
                 </div>
             </form>
         </div>
         {{form.media}}
     {% endblock %}
 }}}
 urls.py
 {{{
 from django.contrib.admin.views.decorators import staff_member_required
 from django.urls import path

 from polls.views import QuestionExportView

 app_name = "question_exports"


 urlpatterns = [
     path("admin/question/",
 staff_member_required(QuestionExportView.as_view()), name="question"),
     path('admin/', admin.site.urls),
 ]

 }}}

 How can I approach this issue? Any help would be appreciated :)!

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33729#comment:5>
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/01070180ec73ec80-2972a35a-7dd7-45fb-9a3e-d6bb7328b2ed-000000%40eu-central-1.amazonses.com.

Reply via email to